MyNameWouldGoHere MyNameWouldGoHere - 4 months ago 6
jQuery Question

How to "cancel" window.scroll()

So I have a couple of divs with scrollbars in. Obviously, when it gets to the bottom of those divs, the rest of the body starts to scroll. Not ideal.

The "position:fixed;" trick on body doesn't work correctly due to the layout of other divs, so I put together a small solution that sets the window scroll position to the current scroll when hovering over one of the divs. Here's the code:

$(".scrollLock").hover( function() {
var scrollX = window.scrollX, scrollY = window.scrollY;
window.onscroll = function(e){
scroll(scrollX,scrollY)
}
}, function() {
var scrollX = window.scrollX, scrollY = window.scrollY;
window.onscroll = function(e){
scroll()
}
});


Obviously that second
scroll()
isn't correct. Whilst it works, it fills the console with errors as it's expecting two arguments. However, if I remove it, the scroll position stays locked even when I mouseout of the
scrollLock
div. Is there a more correct way of doing this?

Answer

Try e.preventDefault().

Although that may not work for scrolling.