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

后端 未结 20 1504
抹茶落季
抹茶落季 2020-12-01 01:45

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.<

20条回答
  •  感情败类
    2020-12-01 02:06

    Courtesy of JDiApice who synthesized and extended the work of other contributors at iOS 8.x modal scroll issue #14839:

    @media only screen and (max-device-width:768px) {
    
    body.modal-open {
        // block scroll for mobile;
        // causes underlying page to jump to top;
        // prevents scrolling on all screens
        overflow: hidden;
        position: fixed;
        }
    }
    
    body.viewport-lg {
        // block scroll for desktop;
        // will not jump to top;
        // will not prevent scroll on mobile
        position: absolute;
    }
    
    body {
        overflow-x: hidden;
        overflow-y: scroll !important;
    }
    
    /* The reason the media specific is on there is 
       on a desktop i was having issues with when the modal would open 
       all content on the page would shift from centered to left. 
       Looked like crap. So this targets up to tablet size devices 
       where you would need to scroll. There is still a slight shift 
       on mobile and tablet but its really not much. */
    

    Unlike the other solutions we tried, it does not scroll the background to the top after the popup modal closes.

提交回复
热议问题