我在找这样的东西:

$(window).scroll(function(event){
   if (/* magic code*/ ){
       // upscroll code
   } else {
      // downscroll code
   }
});

什么好主意吗?


当前回答

这是在用户结束滚动时检测方向的最佳解决方案。

var currentScrollTop = 0 ;

$(window).bind('scroll', function () {     

    scrollTop = $(this).scrollTop();

    clearTimeout($.data(this, 'scrollTimer'));
    $.data(this, 'scrollTimer', setTimeout(function() {

        if(scrollTop > currentScrollTop){
            // downscroll code
            $('.mfb-component--bl').addClass('mfbHide');
        }else{
            // upscroll code
            $('.mfb-component--bl').removeClass('mfbHide');
        }
        currentScrollTop = scrollTop;

    }, 250));

});

其他回答

在元素的.data()中,您可以存储JSON和测试值来启动事件

{ top : 1,
   first_top_event: function(){ ...},
   second_top_event: function(){ ...},
   third_top_event: function(){ ...},
   scroll_down_event1: function(){ ...},
   scroll_down_event2: function(){ ...}
}

你可以这样做,而不必跟踪之前的滚动顶部,因为所有其他的例子都要求:

$(window).bind('mousewheel', function(event) {
    if (event.originalEvent.wheelDelta >= 0) {
        console.log('Scroll up');
    }
    else {
        console.log('Scroll down');
    }
});

我不是这方面的专家,所以请随意进一步研究,但当您使用$(element)时。滚动,被监听的事件是一个'滚动'事件。

但是如果您使用bind专门监听鼠标滚轮事件,则回调的事件参数的originalEvent属性包含不同的信息。其中一部分信息是wheelDelta。如果是正数,你就向上移动鼠标滚轮。如果是负的,鼠标滚轮就向下移动了。

我的猜测是鼠标滚轮事件将在鼠标滚轮转动时触发,即使页面没有滚动;在这种情况下,'scroll'事件可能不会被触发。如果需要,可以在回调的底部调用event. preventdefault()来阻止页面滚动,这样就可以将鼠标滚轮事件用于页面滚动之外的其他事情,比如某种类型的缩放功能。

var tempScrollTop, currentScrollTop = 0; 

$(window).scroll(function(){ 

   currentScrollTop = $("#div").scrollTop(); 

   if (tempScrollTop > currentScrollTop ) {
       // upscroll code
   }
  else if (tempScrollTop < currentScrollTop ){
      // downscroll code
  }

  tempScrollTop = currentScrollTop; 
} 

或者使用鼠标滚轮扩展,见这里。

在滚动的元素的.data()中存储一个增量,然后就可以测试滚动到达顶部的次数。

现有的解决方案

从这篇帖子和其他答案中可能有3个解决方案。

解决方案1

    var lastScrollTop = 0;
    $(window).on('scroll', function() {
        st = $(this).scrollTop();
        if(st < lastScrollTop) {
            console.log('up 1');
        }
        else {
            console.log('down 1');
        }
        lastScrollTop = st;
    });

解决方案2

    $('body').on('DOMMouseScroll', function(e){
        if(e.originalEvent.detail < 0) {
            console.log('up 2');
        }
        else {
            console.log('down 2');
        }
    });

解决方案3

    $('body').on('mousewheel', function(e){
        if(e.originalEvent.wheelDelta > 0) {
            console.log('up 3');
        }
        else {
            console.log('down 3');
        }
    });

多浏览器测试

我无法在Safari上测试它

chrome 42 (Win 7)

解决方案1 向上:每次滚动1个事件 向下:每滚动1个事件 解决方案2 向上:不工作 向下:不工作 解决方案3 向上:每次滚动1个事件 向下:每滚动1个事件

Firefox 37 (win7)

解决方案1 向上:每次滚动20个事件 向下:每次滚动20个事件 解决方案2 向上:不工作 向下:每滚动1个事件 解决方案3 向上:不工作 向下:不工作

IE 11 (win8)

解决方案1 向上:每一次滚动10个事件(副作用:最后出现向下滚动) 向下:每次滚动10个事件 解决方案2 向上:不工作 向下:不工作 解决方案3 向上:不工作 向下:每滚动1个事件

IE 10 (win7)

解决方案1 向上:每次滚动1个事件 向下:每滚动1个事件 解决方案2 向上:不工作 向下:不工作 解决方案3 向上:每次滚动1个事件 向下:每滚动1个事件

IE 9 (win7)

解决方案1 向上:每次滚动1个事件 向下:每滚动1个事件 解决方案2 向上:不工作 向下:不工作 解决方案3 向上:每次滚动1个事件 向下:每滚动1个事件

IE 8 (win7)

解决方案1 向上:每一次滚动2个事件(副作用:最后出现向下滚动) 向下:每一次滚动2~4个事件 解决方案2 向上:不工作 向下:不工作 解决方案3 向上:每次滚动1个事件 向下:每滚动1个事件


联合解决方案

我检查了ie11和ie8的副作用是来自if else语句。因此,我用if else if语句替换它,如下所示。

从多浏览器测试中,我决定对普通浏览器使用解决方案3,对firefox和IE 11使用解决方案1。

我参考这个答案来检测IE 11。

    // Detect IE version
    var iev=0;
    var ieold = (/MSIE (\d+\.\d+);/.test(navigator.userAgent));
    var trident = !!navigator.userAgent.match(/Trident\/7.0/);
    var rv=navigator.userAgent.indexOf("rv:11.0");

    if (ieold) iev=new Number(RegExp.$1);
    if (navigator.appVersion.indexOf("MSIE 10") != -1) iev=10;
    if (trident&&rv!=-1) iev=11;

    // Firefox or IE 11
    if(typeof InstallTrigger !== 'undefined' || iev == 11) {
        var lastScrollTop = 0;
        $(window).on('scroll', function() {
            st = $(this).scrollTop();
            if(st < lastScrollTop) {
                console.log('Up');
            }
            else if(st > lastScrollTop) {
                console.log('Down');
            }
            lastScrollTop = st;
        });
    }
    // Other browsers
    else {
        $('body').on('mousewheel', function(e){
            if(e.originalEvent.wheelDelta > 0) {
                console.log('Up');
            }
            else if(e.originalEvent.wheelDelta < 0) {
                console.log('Down');
            }
        });
    }