I occasionally need to block mouse wheel scrolling when I’m working with JavaScript. Of course, this is only momentary, such as when I need to display an overlay of some sort.
I usually use MooTools for my scripting needs, but frameworks can sometimes become a handicap. While it makes coding easy, it makes you dependent on it and then you forget the basics.
In this article, I will show you how to disable the mouse wheel by adding mousewheel events and how to remove them when you are done. Each line of code has comments to show which browsers need it.
Solid Tip: This script will not require a JavaScript framework and has been tested in Firefox, Chrome, Safari, IE7, and IE8.
Blocking the Mouse Wheel
document.onmousewheel = function(){ stopWheel(); } /* IE7, IE8 */ if(document.addEventListener){ /* Chrome, Safari, Firefox */ document.addEventListener('DOMMouseScroll', stopWheel, false); } function stopWheel(e){ if(!e){ e = window.event; } /* IE7, IE8, Chrome, Safari */ if(e.preventDefault) { e.preventDefault(); } /* Chrome, Safari, Firefox */ e.returnValue = false; /* IE7, IE8 */ } |
Re-enabling the Wheel
document.onmousewheel = null; /* IE7, IE8 */ if(document.addEventListener){ /* Chrome, Safari, Firefox */ document.removeEventListener('DOMMouseScroll', stopWheel, false); } |
Notice my use of document
in the instead of window
when adding and removing the event. The IE versions would fail using window
, but it does need window.event
. Obviously, we would never expect IE to do it the easy way…
If this article helped you out, leave a comment!
espectacular! valeu funciona mesmo
Tank you very much, it help me in my project
Thank you very much! This solution works perfectly!
Great stuff!
I wrote some JS code for that. But it didn’t work with FF.
Your’s works fine!
Thank you!
excellent script! this has helped me with the development of a parallax site with ajax lightboxes
Glad to help!
it is not working in chrome,safari ie8 browser the mousewheel option is enabled
it works fine in all those browsers
I have tried it on Firefox 14 1.0 and it doesn’t work. Great scrip otherwise.
14.0.1 is the current Firefox release and it works fine.
No honestly I’m trying it on Firefox 14.1.0 and it keeps on scrolling the main page. I’m I missing anything ? I don’t use the snippet of code marked with “Re-enabling the Wheel” as apparently I don’t need to.
Have a go here http://jsfiddle.net/9Htjw/4/
Of course it doesn’t work when you change it like that. You should be using Firebug. It tells you that you have an error in your version, thanks to trying to shoehorn it into JQuery.
I am still wondering where you are getting 14.1.0 from
It may be me doing something wrong. I got things working now using slightly different method. Thanks for the post.
Hi ..Thanks buddy for such information.
It really works.
but here i need to handle even as CTRL + MOUSEWHEEL(that doesn’t work for this case.)
Can any1 help me out to sort out this problem.
Hi Amit, I was hoping someone else might chime in, but I don’t really have the bandwidth to try to help you with this code right now. Doesn’t seem too difficult to add the CTRL key to the equation, though.
Thx !