javascriptjqueryscrollmousewheel

Disable Mouse-wheel to scroll up or down


I have two pages, these are the conditions I cannot achieve, please let me know if it is not possible.

  1. In one page, I need to disable the mouse-wheel to scroll up. So when I scroll up with mouse-wheel nothing happens, but when I scroll down the page scrolls.

  2. In the other page, I want the exact opposite, I need to disable the scrolling down on mouse-wheel. So when I scroll down nothing happens, but when I scroll up the page scrolls.

This is all i really need, but if you think I need to explain more, please let me know, thank you.


Solution

  • Case of preventing mouse scroll down (for mouse scroll up just change comparison operator to '<'):

    $(window).on("wheel mousewheel", function(e){
        if(e.originalEvent.deltaY > 0) {
            e.preventDefault();
            return;
        } else if (e.originalEvent.wheelDeltaY < 0) {
            e.preventDefault();
            return;
        }    
    });
    

    We need to check for two values because of cross-browser issues.

    jsFiddle link

    P. S. Warning. Since later versions of Chrome decides to treat all window events as passive by default, the code above won't work in Chrome. Will come with a better solution and update this answer ASAP.