Bootstrap 3 modal fires and causes page to shift to the left momentarily / browser scroll bar problems

后端 未结 24 1478
遥遥无期
遥遥无期 2020-12-12 12:08

I am working on a site using Bootstrap 3.1.0.

You\'ll notice when the modal window opens, the browser scroll bar just disappears for a split second, then comes back.

24条回答
  •  伪装坚强ぢ
    2020-12-12 13:04

    As of Bootstrap 3.3.2 the behavior appears to be that the scroll bars are removed when the dialog is shown and Bootstrap adds some right padding to the body element to compensate for the space previously taken up by the scroll bar. Unfortunately this doesn't prevent the screen shift, at least in modern versions of Chrome, IE, Firefox or Safari. None of the fixes here completely fix this issue but combining the answers from ben.kaminski and part of the answer from cjd82187 resolves the issue with only CSS:

    /* removes inline padding added by Boostrap that makes the screen shift left */
    .modal-open[style] {
        padding-right: 0px !important;
    }
    
    /* keeps Bootstrap from removing the scrollbar if it's there */
    .modal-open {
        overflow: auto;
    }
    

    As mentioned by ben.kaminski, the code was added to Twitter Bootstrap so you could scroll to bring the modal into view if it's beyond the bottom of the screen. To retain that functionality you can wrap the CSS solution in a media query so it only applies to large viewports, something like this:

    @media (min-width: 992px) {         
        .modal-open[style] {
            padding-right: 0px !important;
        }
    
        .modal-open {
            overflow: auto;
        }   
    }
    

提交回复
热议问题