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.
Bootstrap’s
modalautomatically adds the classmodal-opento the body when a modal dialog is shown and removes it when the dialog is hidden. You can therefore add the following to your CSS:You could argue that the code above belongs to the Bootstrap CSS code base, but this is an easy fix to add it to your site.
Update 8th feb, 2013
This has now stopped working in Twitter Bootstrap v. 2.3.0 — they no longer add the
modal-openclass to the body.A workaround would be to add the class to the body when the modal is about to be shown, and remove it when the modal is closed:
Update 11th march, 2013
Looks like the
modal-openclass will return in Bootstrap 3.0, explicitly for the purpose of preventing the scroll:See this: https://github.com/twitter/bootstrap/pull/6342 – look at the Modal section.