I have a modal box window (pop-up) that contains an iframe,
and inside that iframe there's a div that is scrollable.
When I scroll the iframe's inner DIV, and it has reached its top or bottom limit,
the window of the browser itself starts to scroll. this is an unwanted behavior.
I've tried something like this, which kills the main window scroll when
onMouseEnter when mouse enters pop-up box area:
e.preventDefault() is not working as it should for some reason...
$("#popup").mouseenter(function(){
$(window).bind("scroll", function(e){
e.preventDefault();
});
}).mouseleave(function(){
$(window).unbind("scroll");
});
Update
Seems like now in 2013 e.preventDefault();
is enough...
If we cannot prevent window scrolling, why not undo it? That is, catching the scroll event and then scrolling back to a fixed position.
The following code locks the Y-Axis as long as one hovers over
$("#popup")
:I use this for the query suggest box on http://bundestube.de/ (enter some characters into the top search box to make the scrollable pane visible):
This works flawlessly in Chrome/Safari (Webkit) and with some scrolling glitches in Firefox and Opera. For some reason, it does not work with my IE installation. I guess this has to do with jQuery's hover method, which appears to not work correctly in 100% of all cases.
New web dev here. This worked like a charm for me on both IE and Chrome.
Don't let the number of lines fool you, it is quite simple - just a bit verbose for readability (self documenting code ftw right?)
That's how I solved the problem:
I call the following when I open the popup:
Then, when I close the popup I call this:
The popup is meant to be modal so no interaction is required with the underlying body
Works pretty well
I would like to add a bit updated code that I found to work best:
The difference between this one and one that is already mentioned above is the addition of more events and the usage of outerHeight() instead of height() to avoid crashing if element has padding!
Here's what I do:
demo fiddle
Use CSS
overflow:hidden
to hide the scrollbar as this will do nothing if they drag it.Works cross-browser