4

私はオンライン製品サイトを作成しています。スクロール イベントをトリガーしています。開始時に 12 要素のみが表示されますが、8 番目の要素が一番上にスクロールされると、スクロール イベントは 1 回だけ実行されますが、下にスクロールするたびに実行されます。助けてください. ここに私のコードがあります:

var navigation_offset_top = $('.prods-list li:nth-child(4)').offset().top;  
    $(window).on("scroll",function(){
        var scroll_top = $(window).scrollTop(); // current vertical position from the top
        // if scrolled more than the navigation, change its position to fixed to stick to top, otherwise change it back to relative
        if (scroll_top > sticky_navigation_offset_top) 
        { 
            console.log("Hi");
        }       
    });
4

5 に答える 5

6

とを使用on()off()、スクロールが適切なポイントに達したときにイベント ハンドラーのバインドを解除します。

var navigation_offset_top = $('.prods-list li:nth-child(4)').offset().top;  

$(window).on("scroll", doScroll);

function doScroll() {
    var scroll_top = $(window).scrollTop();
    if (scroll_top > sticky_navigation_offset_top) { 
        console.log("Hi");
        $(window).off('scroll', doScroll);
    }
};
于 2013-03-26T12:39:47.473 に答える
4

unbind()オブジェクトからイベントをバインド解除するために呼び出します。

$(window).on("scroll",function(){
    var scroll_top = $(window).scrollTop(); // current vertical position from the top
    // if scrolled more than the navigation, change its position to fixed to stick to top, otherwise change it back to relative
    if (scroll_top > sticky_navigation_offset_top) 
    { 
        console.log("Hi");
    }       
    $(this).unbind("scroll");
});
于 2013-03-26T12:37:10.813 に答える
3

必要なのは.oneメソッドだと思います

$(window).one("scroll",function(){
    var scroll_top = $(window).scrollTop(); // current vertical position from the top
    // if scrolled more than the navigation, change its position to fixed to stick to top, otherwise change it back to relative
    if (scroll_top > sticky_navigation_offset_top) 
    { 
        console.log("Hi");
    }       
});

詳細については、http: //api.jquery.com/one/をご覧ください。

于 2013-03-26T12:41:06.323 に答える
1

完了後にイベントのバインドを解除できます。

お気に入り $(this).unbind("scroll);

于 2013-03-26T12:37:40.707 に答える
-1
var thisHash = window.location.hash;
jQuery(document).ready(function() {

    if(window.location.hash) {
      jQuery.fancybox(thisHash, {
        padding: 20
        // more API options
      });
    }

});

ファンシーボックスをトリガーするボタンが必要ないため、これはさらに優れています。また、以前のコードでは、fancybox 内にフォームを挿入できませんでした。フォーム内をクリックするたびに、fancybox が再起動していたからです。

于 2015-04-29T00:08:20.800 に答える