I have a bootstrap v3 modal, like in the documentation.
When I open the modal for a first time ('Launch demo model' button) a vertical scroll bar is in a top position. If I scroll down and click 'Cancel' in modal it will close. It is ok. But if I open modal again it will be already scrolled down. And this is case which I would like to avoid. What should I do to open modal in a top position?
Approach: A simple solution to this problem is to set the value of the “overflow” property of the body element to “hidden” whenever the modal is opened, which disables the scroll on the selected element.
style['-webkit-transform'] = 'translate3d(0px, 0px, 0px)'; } } } resetScrollPos('. mblScrollableViewContainer'); Calling this function after transition between view ,will reset my scroll position.
Bootstrap 4.3 added new built-in scroll feature to modals. This makes only the modal-body content scroll if the size of the content would otherwise make the page scroll. To use it, just add the class modal-dialog-scrollable to the same div that has the modal-dialog class.
$('#my-modal').on('shown.bs.modal', function () {
$('#my-modal').scrollTop(0);
});
I think you would need to to use the events that fire when the modal is opened to reset the scrolling.
Something like:
$('#myModal').on('shown.bs.modal', function () {
window.scrollTo(0,0);
});
Depending on your page you might wantto use some jQuery or animations:
$('#myModal').on('shown.bs.modal', function () {
$('html, body').animate({
scrollTop: $("#myModal").offset().top
}, 1000);
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With