0

HTMLページがウィンドウの上部にあるかどうかを確認する必要があります。

だから、私はこのコードを使用しています:

$(window).scroll(function(){
   a = ($(window).scrollTop());
   if (a>0) {
      alert('page not in top');
   }
});

ただし、ユーザーがスクロール アクションを停止したときにのみイベントが発生する必要があるため、これは期待どおりに機能しません。何か案が?

4

2 に答える 2

1

使用setTimeout:

var timeout;

$(window).scroll(function() {
   clearTimeout(timeout);
   timeout = setTimeout(function(){
      a = $(window).scrollTop();
      if ( a > 0 ) {
           alert('page not in top');
      }
   }, 100);
});
于 2013-05-30T00:15:30.653 に答える
1

これを試して:

var timer = null;
$(window).addEventListener('scroll', function() {
    if(timer !== null) {
        clearTimeout(timer);        
    }
    timer = setTimeout(function() {
          // do something
    }, 150);
}, false);

またはこれ:

    var timer;
    $(window).bind('scroll',function () {
        clearTimeout(timer);
        timer = setTimeout( refresh , 150 );
    });
    var refresh = function () { 
        // do stuff
        console.log('Stopped Scrolling'); 
    };
于 2013-05-30T00:16:02.937 に答える