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

115,763

Solution 1

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/

Solution 2

Try this,

 body.modal-open {
    overflow: hidden;
    position:fixed;
    width: 100%;
}

Solution 3

I tried the accepted answer which prevented the body from scrolling but had the issue of scrolling to the top. This should solve both issues.

As a side note, it appears overflow:hidden doesn't work on body for iOS Safari only as iOS Chrome works fine.

var scrollPos = 0;

$('.modal')
    .on('show.bs.modal', function (){
        scrollPos = $('body').scrollTop();
        $('body').css({
            overflow: 'hidden',
            position: 'fixed',
            top : -scrollPos
        });
    })
    .on('hide.bs.modal', function (){
        $('body').css({
            overflow: '',
            position: '',
            top: ''
        }).scrollTop(scrollPos);
    });

Solution 4

$('.modal') 
.on('shown', function(){ 
  console.log('show'); 
  $('body').css({overflow: 'hidden'}); 
}) 
.on('hidden', function(){ 
  $('body').css({overflow: ''}); 
}); 

use this one

Solution 5

No scripts needed.

BS 3 sets a .modal-open class on body that you can use to set the position and overflow values (made with LESS).

body {
    font-family:'Open Sans';
    margin:0;

    &.modal-open {
        position:fixed;
        overflow:hidden;

        .modal { 
            overflow: scroll;

            @media only screen and (min-resolution:150dpi) and (max-width: @screen-sm),
            only screen and (-webkit-min-device-pixel-ratio:1.5) {
                overflow: scroll; 
                -webkit-overflow-scrolling: touch; 
            }
        }
    }   
}
Share:
115,763
fat fantasma
Author by

fat fantasma

Updated on July 09, 2022

Comments

  • fat fantasma
    fat fantasma almost 2 years

    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?

  • fat fantasma
    fat fantasma over 10 years
    thanks, that was it. I had some left over code from BS 2 messing me up.
  • Fedor
    Fedor almost 10 years
    too verbose, I think.
  • Michal Stefanow
    Michal Stefanow about 9 years
    I actually posted the issue here - github.com/twbs/bootstrap/issues/15852 - body.modal-open { position: fixed; } has a side effect of scrolling to the top... (still not sure what is the best way)
  • Julian Dormon
    Julian Dormon about 9 years
    Michael Stefanow did you figure out the scrolling to top issue by any chance? I am having the same issue with this fix.
  • Tim Rijavec
    Tim Rijavec almost 9 years
    Maybe this would be a better choice hence the page doesn't scrolls back up: $('body').css({"position": "fixed", 'top': (-scrollTo) + 'px'});
  • Ryan Charmley
    Ryan Charmley almost 9 years
    Fix does not seem to work. Jump to top happens when the position is set back to static given the need for the 0 second animate.
  • Tim Rijavec
    Tim Rijavec almost 9 years
    So, given a global variable var scrollTo; you can have two functions function beforeModalOpen() { scrollTo = $('body').scrollTop(); $('body').css({"position": "fixed", 'top': (-scrollTo) + 'px'}); } and function afterModalClose() {$('body').css("position", "static"); $('body').animate({scrollTop: scrollTo}, 0);} and call them appropriately.
  • Ade
    Ade over 8 years
    one thing to maintain the scroll position I did the following when you load the overlay save your scroll position [var scrollTop = document.getElementById("body").scrollTop] when you close the overlay restore your position with [document.getElementById("body").scrollTop = scrollTop;]
  • Perry
    Perry over 8 years
    V3 Did not fix this issue
  • madzohan
    madzohan over 8 years
    @AntonK hm, I've simply put thing which worked for me :) thanks for downvote lol
  • vborutenko
    vborutenko over 8 years
    Has a side effect of scrolling to the top
  • Ujjwal Kumar Gupta
    Ujjwal Kumar Gupta over 8 years
    can you please provide a demo
  • WitVault
    WitVault about 8 years
    is it working? Please provide some demo if possible. Thanks.
  • m.spyratos
    m.spyratos over 7 years
  • Mark Pruce
    Mark Pruce over 7 years
    I posted below a solution that fixed the scroll to top issue - similar to what @Ade says above, but a little more detailed.
  • iamse7en
    iamse7en about 7 years
    Thank you. Worked great for me, tested on desktop and ios. My last issue is trying to prevent scrolling to top on iOS after I an ajax action. Doesn't scroll to top on desktop.
  • iamse7en
    iamse7en about 7 years
    nevermind, adding $('body').scrollTop(lastKnownScrollTopPosition); to my x.js.erb file as well (the file that executes js code upon submit) worked great. thanks!
  • Hubert
    Hubert almost 7 years
    To fix the right margin, add padding with width equal to the width of the scrollbar: stackoverflow.com/a/19015262/4160062
  • bkwdesign
    bkwdesign over 6 years
    no negative effects on desktop, does improve modal experience on my iPhone.. but, upon closing my modal, the original screen has scrolled-to-top
  • bkwdesign
    bkwdesign over 6 years
    I'm using your solution, minus your line of code position: fixed
  • Praveen
    Praveen over 6 years
    but when i enter something in text field and then, when i try to scroll up or down the body content scrolls again.
  • Mugur 'Bud' Chirica
    Mugur 'Bud' Chirica over 6 years
    Can you create a jsfiddle?
  • Admin
    Admin over 6 years
    Does not work - chrome Version 63.0.3239.132 Still scrolls background.
  • kmgdev
    kmgdev almost 6 years
    Adding this fixed my issue where dynamically-sized content inside a modal window was not scrolling.
  • Mugur 'Bud' Chirica
    Mugur 'Bud' Chirica almost 6 years
    Glad to hear that.
  • Mark Salvania
    Mark Salvania almost 4 years
    This helps for mobile users especially for IOS