Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to reset scroll position of modal in bootstrap3?

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?

like image 395
Dmitry Gorshkov Avatar asked Oct 30 '13 13:10

Dmitry Gorshkov


People also ask

How do I stop modal background scrolling?

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.

How do you reset scrolling in HTML?

style['-webkit-transform'] = 'translate3d(0px, 0px, 0px)'; } } } resetScrollPos('. mblScrollableViewContainer'); Calling this function after transition between view ,will reset my scroll position.

How do I make my modal body scrollable?

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.


2 Answers

working in production

$('#my-modal').on('shown.bs.modal', function () {
    $('#my-modal').scrollTop(0);
});
like image 74
King Friday Avatar answered Sep 18 '22 15:09

King Friday


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);
});
like image 37
finnmich Avatar answered Sep 17 '22 15:09

finnmich