简体   繁体   中英

Why bootstrap modal removes my scroll bar after closing modal and anchoring to page

I have a bootstrap modal and and button inside it aside from the close button. So what I want is when I click the button, the modal will close and will navigate to the same page via anchor tag. This works but problem is the modal is removing or disabling the scroll bar. The modal thinks as if the modal is still open.

I have this:

$( "#silverB" ).click(function() {
  $("#myModal").hide();
  $(document.body).scrollTop($('#optin').offset().top);
});

You should not retire the modal using .hide() . Instead use this:

$("#myModal").modal("hide");

Because, bootstrap adds more classes to body ( .modal-open ) when the modal is opened. But when you "hide" the modal, they are not reset.

From the docs :

.modal('hide')

Manually hides a modal. Returns to the caller before the modal has actually been hidden (ie before the hidden.bs.modal event occurs).`

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM