How to determine scroll direction without actually scrolling

后端 未结 6 1339
走了就别回头了
走了就别回头了 2020-11-30 02:09

I am coding a page where the first time the user scrolls, it doesn\'t actually scroll the page down, instead it adds a class with a transition. I\'d like to detect when the

6条回答
  •  离开以前
    2020-11-30 02:49

    The mousewheel event is quickly becoming obsolete. You should use wheel event instead.

    This would also easily allow you to the vertical and/or horizontal scroll direction without scroll bars.

    This event has support in all current major browsers and should remain the standard far into the future.

    Here is a demo:

    window.addEventListener('wheel', function(event)
    {
     if (event.deltaY < 0)
     {
      console.log('scrolling up');
      document.getElementById('status').textContent= 'scrolling up';
     }
     else if (event.deltaY > 0)
     {
      console.log('scrolling down');
      document.getElementById('status').textContent= 'scrolling down';
     }
    });

提交回复
热议问题