检测鼠标滚轮滚动的次数

Detect number of times mousewheel is scrolled

本文关键字:滚动 鼠标 检测      更新时间:2023-09-26

我正在尝试实现像这里这样的效果。我想在每次滚动滚轮时执行一个操作,无论用户尝试滚动多少。如何计算用户尝试滚动的次数?我一直在玩$(window).on('scroll'....谢谢

你的意思是这样算数的东西吗:

(function() {
    var scroll = 0;
    $(document).on('mousewheel DOMMouseScroll', function(event) {
        scroll++;
        console.log(event);
    });
    $('#click').on('click', function() {
        alert(scroll);
    });
})();
<button id="click">Show me</button>

http://jsfiddle.net/eHEmr/

(你显然可以放自己的代码而不是scroll++;

事实证明,

我真正需要的是找到滚动何时停止。在这里得到了解决方案。

var scrolls = 0;
$(window).scroll(function(){
    clearTimeout($.data(this, 'scrollTimer'));
    $.data(this, 'scrollTimer', setTimeout(function() {
        pos++;
    }, 50));
});

谢谢