59

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

Daniel Ortiz
  • 901
  • 2
  • 7
  • 14
  • note that `mousewheel` is deprecated and non-standard. Although only FireFox does not support it. To support FireFox, you can try handling the `DOMMouseScroll`, the equivalent of `e.wheelDelta` in `mousewheel` event handler is about `-40*e.detail` in `DOMMouseScroll` event handler. Also looks like jQuery removes the property, you have to access to the `originalEvent`. – King King Jun 14 '14 at 06:21
  • you may use the **[wheel event](https://developer.mozilla.org/en-US/docs/Web/Events/wheel)**. Here you can check an example with cross-browser support, handling the scrolling: **[stackoverflow.com/questions/4989632...](http://stackoverflow.com/questions/4989632/differentiate-between-scroll-up-down-in-jquery/24792018#24792018)** – jherax Jul 17 '14 at 03:22

6 Answers6

147

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>
John
  • 1
  • 13
  • 98
  • 177
www139
  • 4,960
  • 3
  • 31
  • 56
23

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.

cracker
  • 4,900
  • 3
  • 23
  • 41
  • 1
    Thank you so much, worked like a charm. Here's a not shortened version just in case someone else need it (that way it my be difficult to read for beginners) – Daniel Ortiz Jun 14 '14 at 15:56
  • 3
    As mentioned in www139's answer, the `mousewheel` event is [depreciated](https://developer.mozilla.org/en-US/docs/Web/Events/mousewheel). You should use the [`wheel` event](https://developer.mozilla.org/en-US/docs/Web/Events/wheel) instead (again, see www139's answer). –  Apr 29 '17 at 17:12
4

I know this post is from 5 years ago but I didn't see any good Jquery answer (the .on('mousewheel') doesn't work for me...)

Simple answer with jquery, and use window instead of body to be sure you are taking scroll event :

$(window).on('wheel', function(e) {
    var scroll = e.originalEvent.deltaY < 0 ? 'up' : 'down';
    console.log(scroll);
});
NoxFly
  • 179
  • 1
  • 12
3

Try using e.wheelDelta

var animationIsDone = false, scrollDirection = 0;

function preventScroll(e) {

    e.preventDefault();
    e.stopPropagation();
}

$('body').on('mousewheel', function(e) {

    if (e.wheelDelta >= 0) {
        console.log('Scroll up'); //your scroll data here
    }
    else {
        console.log('Scroll down'); //your scroll data here
    }
    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);

    }


});

Note: remember that MouseWheel is deprecated and not supported in FireFox

user3719477
  • 110
  • 9
2

this one work in react app

<p onWheel={this.onMouseWheel}></p> 

after add event listener, in function u can use deltaY To capture mouse Wheel

onMouseWheel = (e) => {
 e.deltaY > 0 
   ? console.log("Down")
   : console.log("up")
}
S.Saderi
  • 4,755
  • 3
  • 21
  • 23
1

Tested on chrome and

$('body').on('mousewheel', function(e) {

    if (e.originalEvent.deltaY >= 0) {
        console.log('Scroll up'); //your scroll data here
    }
    else {
        console.log('Scroll down'); //your scroll data here
    }

});
Yoannes Geissler
  • 791
  • 1
  • 9
  • 18