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.
I just done it this way ...
But when the scroller dissapeared it moved everything right 20px, so i added
straight after it.
Works for me.
You need to go beyond @charlietfl's answer and account for scrollbars, otherwise you may see a document reflow.
Opening the modal:
body
widthbody
overflow tohidden
Explicitly set the body width to what it was in step 1.
Closing the modal:
body
overflow toauto
Set
body
width toauto
Inspired by: http://jdsharp.us/jQuery/minute/calculate-scrollbar-width.php
worked for me
For those wondering how to get the scroll event for the bootstrap 3 modal:
This is the best solution for me:
Css:
And Js:
The accepted answer doesn't work on mobile (iOS 7 w/ Safari 7, at least) and I don't want MOAR JavaScript running on my site when CSS will do.
This CSS will prevent the background page from scrolling under the modal:
However, it also has a slight side-affect of essentially scrolling to the top.
position:absolute
resolves this but, re-introduces the ability to scroll on mobile.If you know your viewport (my plugin for adding viewport to the
<body>
) you can just add a css toggle for theposition
.I also add this to prevent the underlying page from jumping left/right when showing/hiding modals.
this answer is a x-post.