jquery 检测鼠标滚轮滚动的次数

v1l68za4  于 11个月前  发布在  jQuery
关注(0)|答案(3)|浏览(160)

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

mwyxok5s

mwyxok5s1#

你是说像这样的东西:

(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/
(you显然,你可以把自己的代码,而不是scroll++;

amrnrhlw

amrnrhlw2#

原来我真正需要的是找到滚动停止的时间。得到了解决方案here

var scrolls = 0;

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

字符串
谢谢

qvk1mo1f

qvk1mo1f3#

鼠标滚轮计数器

let wheelCounter = 0;

    // Function to handle mouse wheel events
    function handleMouseWheel(event) {
      // Detect the direction of the mouse wheel
      const direction = event.deltaY > 0 ? 'down' : 'up';

      // Update the counter based on the direction
      if (direction === 'down') {
        wheelCounter++;
      } else {
        wheelCounter--;
      }

      // Display the current count
      console.log(`Mouse wheel turned ${wheelCounter} times`);
    }

    // Attach the event listener to the document
    document.addEventListener('wheel', handleMouseWheel);

字符串

相关问题