Search code examples

Prevent BODY from scrolling when a modal is opened

I want my body to stop scrolling when using the mousewheel while the Modal (from 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; });


$(window).live('scroll', function() { return false; });

Please note our website dropped support for IE6, IE7+ needs to be compatible though.


  • Bootstrap's modal automatically adds the class modal-open to 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:

    body.modal-open {
        overflow: hidden;

    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-open class 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:

    $("#myModal").on("show", function () {
    }).on("hidden", function () {

    Update 11th march, 2013 Looks like the modal-open class will return in Bootstrap 3.0, explicitly for the purpose of preventing the scroll:

    Reintroduces .modal-open on the body (so we can nuke the scroll there)

    See this: - look at the Modal section.