This has been a popular question so I am updating to give an overview of the answers provided here and which may be best for you. There are three unique solutions included. Two are from Amos and one is from myself. However, each operates differently.
overflow:hidden
on body. This is simple and works great. But the main window's scrollbars will flash in and out.http://jsfiddle.net/eXQf3/371/
The code works as follows:
$('#abs').bind('mousewheel DOMMouseScroll', function(e) {
var scrollTo = null;
if (e.type == 'mousewheel') {
scrollTo = (e.originalEvent.wheelDelta * -1);
}
else if (e.type == 'DOMMouseScroll') {
scrollTo = 40 * e.originalEvent.detail;
}
if (scrollTo) {
e.preventDefault();
$(this).scrollTop(scrollTo + $(this).scrollTop());
}
});
Changelog:
You cannot disable window scroll, there is a simple workaround though:
//set the overflow to hidden to make scrollbars disappear
$('#container').hover(function() {
$("body").css("overflow","hidden");
}, function() {
$("body").css("overflow","auto");
});
See demo: http://jsfiddle.net/9Htjw/
UPDATE
You can disable the mouse wheel though.
$('#container').hover(function() {
$(document).bind('mousewheel DOMMouseScroll',function(){
stopWheel();
});
}, function() {
$(document).unbind('mousewheel DOMMouseScroll');
});
function stopWheel(e){
if(!e){ /* IE7, IE8, Chrome, Safari */
e = window.event;
}
if(e.preventDefault) { /* Chrome, Safari, Firefox */
e.preventDefault();
}
e.returnValue = false; /* IE7, IE8 */
}
Source: http://solidlystated.com/scripting/javascript-disable-mouse-wheel/
Demo: http://jsfiddle.net/9Htjw/4/
mrtsherman: if you bind the event like this, amosrivera's code works also for firefox:
var elem = document.getElementById ("container");
if (elem.addEventListener) {
elem.addEventListener ("mousewheel", stopWheel, false);
elem.addEventListener ("DOMMouseScroll", stopWheel, false);
}
else {
if (elem.attachEvent) {
elem.attachEvent ("onmousewheel", stopWheel);
}
}
I used nicescroll on my page no method worked. I realized the nicescroller is calling the scroll event and had to temporarily disable nicescroll when hovering the element.
Solution: Temporarily disable nicescroll when hovering an element
$('body').on('mouseover', '#element', function() {
$('body, html').css('overflow', 'auto').getNiceScroll().remove();
}).on('mouseout', '#element', function() {
$('body, html').css('overflow', 'hidden').niceScroll();
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With