Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to prevent background scrolling when Bootstrap 3 modal open on mobile browsers?

How to prevent background scrolling when Bootstrap 3 modal open on mobile platforms? On desktop browsers the background is prevented from scrolling and works as it should.

On mobile browsers (Safari ios, Chrome ios, etc), when a modal is opened and using your finger to scroll it, the background also scrolls as does the modal. How do I prevent that?

like image 354
fat fantasma Avatar asked Sep 27 '13 21:09

fat fantasma


People also ask

How do you stop background scrolling when Bootstrap 3 modal open?

If we know the top of the scroll location and add it to our CSS, then the body will not scroll back to the top of the screen, so problem solved. We can use JavaScript for this by calculating the scroll top, and add that value to the body styles: // When the modal is shown, we want a fixed body document.

How do I disable background scrolling when pop modal is open?

Approach: A simple solution to this problem is to set the value of the “overflow” property of the body element to “hidden” whenever the modal is opened, which disables the scroll on the selected element.

How do I turn off background scrolling?

Disabling scroll with only CSS There's another way to disable scrolling that is commonly used when opening modals or scrollable floating elements. And it is simply by adding the CSS property overflow: hidden; on the element you want to prevent the scroll.

What prevents the body from scrolling when overlay is on?

Approach: To prevent body scrolling but allow overlay scrolling we have to switch the overflow to be hidden when the overlay is being shown. And while the whole page's overflow is hidden or the scroll is disabled the overflow in the y-axis of the overlay is enabled and the overflow can be scrolled.


2 Answers

See here: https://github.com/twbs/bootstrap/issues/7501

So try:

$('body').css('overflow','hidden'); $('body').css('position','fixed'); 

V3.0.0. should have fixed this issue. Do you use the latest version? If so post an issue on https://github.com/twbs/bootstrap/

like image 160
Bass Jobsen Avatar answered Oct 05 '22 08:10

Bass Jobsen


Try this,

 body.modal-open {     overflow: hidden;     position:fixed;     width: 100%; } 
like image 27
Sivakumar Avatar answered Oct 05 '22 10:10

Sivakumar