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

后端 未结 24 1508
遥遥无期
遥遥无期 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 12:53

    this is what i found in github when i search about this problem and for me it works fine

    js:

        $(document).ready(function () {
        $('.modal').on('show.bs.modal', function () {
            if ($(document).height() > $(window).height()) {
                // no-scroll
                $('body').addClass("modal-open-noscroll");
            }
            else {
                $('body').removeClass("modal-open-noscroll");
            }
        });
        $('.modal').on('hide.bs.modal', function () {
            $('body').removeClass("modal-open-noscroll");
        });
    })
    

    css use this css if you have nav-fixed-top and navbar-fixed-bottom:

    body.modal-open-noscroll
    {
        margin-right: 0!important;
        overflow: hidden;
    }
    .modal-open-noscroll .navbar-fixed-top, .modal-open .navbar-fixed-bottom
    {
        margin-right: 0!important;
    }
    

    or user this css if you have navbar-default

    body.modal-open-noscroll 
    {
      margin-right: 0!important;
      overflow: hidden;
    }
    .modal-open-noscroll .navbar-default, .modal-open .navbar-default 
    {
      margin-right: 0!important;
    }
    

提交回复
热议问题