Search code examples
htmltwitter-bootstrapresponsive-designtwitter-bootstrap-3

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


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. It does the same when you close it.

How can I make it so that it just open and closes with no browser scroll bar interaction. I have seen posts on stack where people were having problems, but I can't find an answer specific to my problem, so if someone else has made this request and someone knows about it and wants to link me to it, I'd appreciate it. I've searched for about 2 hours for this before posting up.


Solution

  • 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;
    }