1
<script type="text/javascript">
$(document).ready(function () {  
  var top = $('#rt_outer').offset().top - parseFloat($('#rt_outer').css('marginTop').replace(/auto/, 0));
  $(window).scroll(function (event) {
    // what the y position of the scroll is
    var y = $(this).scrollTop();

    // whether that's below the form
    if (y >= top) {
      // if so, ad the fixed class
      $('#rt_outer').addClass('fixed');
    } else {
      // otherwise remove it
      $('#rt_outer').removeClass('fixed');
    }
  });
});
</script>

問題はかなり簡単です。ページ内の新しい場所へのヘッダー リダイレクトを使用する場合 (たとえば、途中で、header("somepage.php#halfway") を使用して php スクリプトを処理した後に #halfway にジャンプします) このスクリプトによって処理される div コンテナーが勝ちましたページがいずれかの方向にスクロールされるまで下にジャンプしないでください。

これには解決策があることは知っていますが、何がわからないだけです。

4

2 に答える 2

0

Window の hashchange イベントを使用できます。

$(window).on("hashchange", function () {
   console.log('hash changed'); 
});​

ソリューションでは、スクロール コードを関数に抽出し、スクロール ハンドラーと hashchange ハンドラーの両方から関数を呼び出すことができます。

于 2012-09-13T21:35:17.320 に答える
0

John Koerner の回答に基づいて、少し調査を行い、解決策を考え出すことができました。http://www.colonelcoon.net?override=true/で動作を確認できます。

これが私が最終的に使用したコードです...

<script src="http://github.com/cowboy/jquery-hashchange/raw/v1.3/jquery.ba-hashchange.js"></script>
<script type="text/javascript">
$(document).ready(function () {

var top = $('#rt_outer').offset().top - parseFloat($('#rt_outer').css('marginTop').replace(/auto/, 0));

  function checkScroll() {
    // what the y position of the scroll is
    var y = $(this).scrollTop();

    // whether that's below the form
    if (y >= top) {
      // if so, ad the fixed class
      $('#rt_outer').addClass('fixed');
    } else {
      // otherwise remove it
      $('#rt_outer').removeClass('fixed');
    }
  }

  $(window).hashchange(function (event) {
      checkScroll();
  });

  $(window).scroll(function (event) {
      checkScroll();
  });

  $(window).hashchange();
});
</script>
于 2012-09-14T13:19:28.753 に答える