最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Disable Mouse-wheel to scroll up or down - Stack Overflow

programmeradmin1浏览0评论

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.

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.

Share Improve this question asked Sep 18, 2014 at 9:45 BondsmithBondsmith 1,5783 gold badges14 silver badges28 bronze badges 1
  • Not really, I need to disable separate directions of mouse-wheel. – Bondsmith Commented Sep 18, 2014 at 10:25
Add a comment  | 

4 Answers 4

Reset to default 10

This code works for div with id "mydiv". You can change the mydiv to the body or any other element you want. The code works in all browsers.

JS:

var mydiv = document.getElementById("mydiv");
if (mydiv.addEventListener) {
    // IE9, Chrome, Safari, Opera
    mydiv.addEventListener("mousewheel", MouseWheelHandler, false);
    // Firefox
    mydiv.addEventListener("DOMMouseScroll", MouseWheelHandler, false);
}
// IE 6/7/8
else 
    mydiv.attachEvent("onmousewheel", MouseWheelHandler);


function MouseWheelHandler(e) {

    // cross-browser wheel delta
    var e = window.event || e; // old IE support
    var delta = Math.max(-1, Math.min(1, (e.wheelDelta || -e.detail)));

    if(delta==1)         // if mouse scrolls up
    {
         alert('up');  
    }
    if(delta==-1)        // if mouse scrolls down, we disable scrolling.
    {
        e.preventDefault();
        e.stopPropagation();
        return false;
    }
    return false;
}

NOTE: Remember to set overflow to auto or scroll for this function to work.

A working example: Click Here

Hope this helps.

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.

Starting with version 52, Chrome introduced an optional support flag, and soon default support, for passive scroll event listeners, so according to this document, for disable scroll it's enough to specify that your event handler is not passive (passive: false):

    window.addEventListener('mousewheel', e => {
      e.preventDefault();
      yourCustomFn();
    }, { passive: false })

NOTE: for old browsers you need to use polyfill

You could try this to disable mouse scroll. I understand this is not as per your question but I believe it could help you.

JS:

function disableMouseScroll() {
    return false;
}
document.onmousewheel=disableMouseScroll;

Link

发布评论

评论列表(0)

  1. 暂无评论