Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to determine scroll direction without actually scrolling

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 user is scrolling down, because if he scrolls up, I want it to do something else. All the methods that I've found are based on defining the current body ScrollTop, and then comparing with the body scrollTop after the page scrolls, defining the direction, but since the page doesn't actually scroll, the body scrollTop() doesn't change.

animationIsDone = false;  function preventScroll(e) {      e.preventDefault();     e.stopPropagation(); }  $('body').on('mousewheel', function(e) {      if (animationIsDone === false) {         $("#main-header").removeClass("yellow-overlay").addClass("yellow-overlay-darker");         $(".site-info").first().addClass("is-description-visible");         preventScroll(e);          setTimeout(function() {             animationIsDone = true;         }, 1000);      }   }); 

This is what I have come with, but that way it doesn't matter the direction I scroll it triggers the event

like image 915
Daniel Ortiz Avatar asked Jun 14 '14 06:06

Daniel Ortiz


People also ask

What is reverse scroll?

Windows assumes that the page is static and user needs to scroll the viewport(display), on the portion of page, user wants to see. If user wants to see lower part of the page, scroll down and vice-versa.

Which way is natural scrolling?

With natural scrolling, a trackpad or a mouse wheel no longer follows the direction of the scrollbars. Rather, the pointer responds as if your finger were touching the screen.

Why does my scroll wheel go the opposite direction?

Fix Your Mouse's Jerky ScrollingChange the mouse battery. Change the mouse scroll settings. Swap the USB ports. Update the mouse's device driver.


2 Answers

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';   }  });
<div id="status"></div>
like image 101
www139 Avatar answered Sep 21 '22 12:09

www139


Try This using addEventListener.

window.addEventListener('mousewheel', function(e){     wDelta = e.wheelDelta < 0 ? 'down' : 'up';     console.log(wDelta); }); 

Demo

Update:

As mentioned in one of the answers, the mousewheel event is depreciated. You should use the wheel event instead.

like image 23
cracker Avatar answered Sep 19 '22 12:09

cracker