Please look at this other question first: Mouse wheel scroll event and the demo from the accepted answer.
How can I reduce the jitter when a scroll event is canceled?
Please look at this other question first: Mouse wheel scroll event and the demo from the accepted answer.
How can I reduce the jitter when a scroll event is canceled?
Demo: http://jsfiddle.net/HUGTL/
Building on the answer to your original question, you could do something like:
var noscroll = document.getElementById('noscroll');
var locked, lockedX, lockedY;
noscroll.addEventListener('mouseover', function (){
locked = true;
lockedX = window.scrollX;
lockedY = window.scrollY;
}, false);
noscroll.addEventListener('mouseout', function (){
locked = false;
}, false);
// changes
$(window).on("mousewheel keydown", function(e){
if(locked === true){
return false;
}
});
$(window).on("scroll", function(e){
if(locked === true){
// add fallback here for other scenarios
}
});
I'm using jQuery to simplify/standardize event handling.
Note that prevent key events may not be desirable; I included that to show how arrow keys could be prevented (you could also check for specific key codes).
I also added a general fallback handler for scenarios not handled by mousewheel/keydown
.
This may not work well (or at all) with touch events. Most importantly, make sure that it doesn't confuse your users or remove expected functionality.
If you only want to handle the mousewheel, it may be much simpler:
$("#noscroll").on("mousewheel", function(e){
return false;
});
Demo: http://jsfiddle.net/HUGTL/3/. However, this doesn't work for key events.