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.
My page needed a modified version of Agni Pradharma's code to keep it from shifting when the modal was shown. I have a fixed nav header and some pages with scroll, some without. Demo here: http://jsbin.com/gekenakoki/1/
I used both the css:
.modal {
overflow-y: auto;
}
.modal-open {
overflow: auto;
}
and the script:
$(document.body)
.on('show.bs.modal', function () {
if (this.clientHeight <= window.innerHeight) {
return;
}
// Get scrollbar width
var scrollbarWidth = getScrollBarWidth();
if (scrollbarWidth) {
$(document.body).css('padding-left', scrollbarWidth);
}
})
.on('hidden.bs.modal', function () {
$(document.body).css('padding-left', 0);
});
function getScrollBarWidth () {
var inner = document.createElement('p');
inner.style.width = "100%";
inner.style.height = "200px";
var outer = document.createElement('div');
outer.style.position = "absolute";
outer.style.top = "0px";
outer.style.left = "0px";
outer.style.visibility = "hidden";
outer.style.width = "200px";
outer.style.height = "150px";
outer.style.overflow = "hidden";
outer.appendChild (inner);
document.body.appendChild (outer);
var w1 = inner.offsetWidth;
outer.style.overflow = 'scroll';
var w2 = inner.offsetWidth;
if (w1 == w2) w2 = outer.clientWidth;
document.body.removeChild (outer);
return (w1 - w2);
};
None of the above worked and others hours of research. But the following code worked perfect with just a bit of CSS. The above would not remove inline style padding:17px; with JS or jquery, I could add 0 padding, but had no effect.
.modal-open {
padding-right: 0px !important;
overflow-y: scroll;
width: 100%;
display: block;
}
.modal {
position: fixed;
top: 0;
left: 0;
z-index: 1050;
display: none;
width: 100%;
height: 100%;
outline: 0;
padding-right: 0 !important;
}
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;
}
.modal {
overflow-y: auto;
}
.modal-open {
overflow: auto;
}
Will get rid of it. This was added to make the modals work more responsively, so you could scroll down and see a modal if the screen was too short. It also stops the background from being scrollable while a modal is up. If you don't need that functionality, then you can use that css I posted.
Some more info: They are setting .modal-open on the body, which prevents all scrolling on the body and hides the body scrollbar. Then, when the modal comes up it has the dark background that takes up the whole screen, and that has overflow-y: scroll which forces the scrollbar to come back so if the modal extended passed the bottom of the screen you can still scroll the dark background and see the rest of it.
This solution work for me!!!!
.modal {
overflow-y: auto;
}
.modal-open {
overflow: auto;
}
.modal-open[style] {
padding-right: 0px !important;
}
if anyone happens to be using react-bootstrap
the solution is just a bit more complex because react-bootstrap
applies inline styles to the body
element to manipulate the overflow
and padding
styles. This means you must override those inline styles with !important
body.modal-open {
// enable below if you want to additionally allow scrolling with the modal displayed
// overflow: auto !important;
// prevent the additional padding from being applied
padding: 0 !important;
}
NOTE: if you do not enable scrolling (i.e. make the scrollbar visible) by uncommenting the overflow
style, then your page content will appear to shift over by the scrollbar width (if the scrollbar was previously visible that is).