If I remember correctly, the overflow setting: hidden on the body did not work in all browsers that I tested for the modal library that I created for the mobile site. In particular, I had problems preventing body scrolling in addition to modal scrolling, even when I was overflowing: hidden on the body.
For my current site, I ended up doing something like this. It basically just saves the current scroll position in addition to setting “overflow” to “hidden” on the body of the page, and then restores the scroll position after closing the modal mode. There is a condition where another modal botstrap opens, and one is already active. Otherwise, the rest of the code should be clear. Please note that if overflow: hidden on the body does not prevent the window from scrolling for a given browser, this at least sets the initial scroll location on exit.
function bindBootstrapModalEvents() { var $body = $('body'), curPos = 0, isOpened = false, isOpenedTwice = false; $body.off('shown.bs.modal hidden.bs.modal', '.modal'); $body.on('shown.bs.modal', '.modal', function () { if (isOpened) { isOpenedTwice = true; } else { isOpened = true; curPos = $(window).scrollTop(); $body.css('overflow', 'hidden'); } }); $body.on('hidden.bs.modal', '.modal', function () { if (!isOpenedTwice) { $(window).scrollTop(curPos); $body.css('overflow', 'visible'); isOpened = false; } isOpenedTwice = false; }); }
If you don't like this, another option would be to assign max-height and overflow: auto to.modal-body as follows:
.modal-body { max-height:300px; overflow:auto; }
In this case, you can adjust the maximum height for different screen sizes and leave overflow: automatically for different screen sizes. You would have to make sure that the modal header, footer and body are not larger than the screen size, so I would include this part in your calculations.
Daniel Jul 22 '14 at 23:21 2014-07-22 23:21
source share