I want my body to stop scrolling when using the mousewheel while the Modal (from http://twitter.github.com/bootstrap) on my website is opened.
I've tried to call the piece of javascript below when the modal is opened but without success
$(window).scroll(function() { return false; });
AND
$(window).live('scroll', function() { return false; });
Please note our website dropped support for IE6, IE7+ needs to be compatible though.
This worked for me:
I had a sidebar that was generated by checkbox hack. But the main idea is to save the document scrollTop and not to change it during scrolling the window.
I just didn't like the page jumping when body becomes 'overflow: hidden'.
As of November 2017 Chrome Introduced a new css property
overscroll-behavior: contain;
which solves this problem although as of writing has limited cross browser support.
see below links for full details and browser support
Warning: The option below is not relevant to Bootstrap v3.0.x, as scrolling in those versions has been explicitly confined to the modal itself. If you disable wheel events you may inadvertently prevent some users from viewing the content in modals that have heights greater than the viewport height.
Yet Another Option: Wheel Events
The scroll event is not cancelable. However it is possible to cancel the mousewheel and wheel events. The big caveat is that not all legacy browsers support them, Mozilla only recently adding support for the latter in Gecko 17.0. I don't know the full spread, but IE6+ and Chrome do support them.
Here's how to leverage them:
JSFiddle
Couldn't make it work on Chrome just by changing CSS, because I didn't want the page to scroll back to the top. This worked fine:
Sadly none of the answers above fixed my issues.
In my situation, the web page originally has a scroll bar. Whenever I click the modal, the scroll bar won't disappear and the header will move to right a bit.
Then I tried to add
.modal-open{overflow:auto;}
(which most people recommended). It indeed fixed the issues: the scroll bar appears after I open the modal. However, another side effect comes out which is that "background below the header will move to the left a bit, together with another long bar behind the modal"Luckily, after I add
{padding-right: 0 !important;}
, everything is fixed perfectly. Both the header and body background didn't move and the modal still keeps the scrollbar.Hope this can help those who are still stuck with this issue. Good luck!