Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get scroll direction without scrolling

Tags:

javascript

How can I detect the scroll direction (mousewheel up/down) without scrolling the page? My page wrapper has a height of 100%, so there is no content to scroll, but I need the scroll direction from the mousewheel.

Since Google Maps does the same (using the mousehweel for zoom without "scrolling" the page), I wonder how to achieve this.

like image 302
Slevin Avatar asked Mar 09 '26 14:03

Slevin


1 Answers

If you are talking about the mouse wheel, you can use addEventListener on the window. event.wheelDelta will have a delta of -120 or 120, for scrolling down and up, respectively.:

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

JSFiddle

Of course, you'll need to cater for different browsers. But i'll leave that up to you. See here

like image 77
George Avatar answered Mar 12 '26 02:03

George



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!