Bootstrap modal: background jumps to top on toggle

Bootstrap modal: background jumps to top on toggle

I have a problem, with a modal. I have a button on a page, that toggles the modal. When the modal shows up, the page jumps to top.
I have done everything I could to find a solution/etc, but I am really lost.
I have also tried with: $(‘#myModal’).modal(‘show’); but it has the exact same effect.


Solution 1:

When the modal opens a modal-open class is set to the <body> tag. This class sets overflow: hidden; to the body. Add this rule to your stylesheet to override the bootstrap.css style:

body.modal-open {
    overflow: visible;

Now the scroll should stay in place.

Solution 2:

If you are calling modal with tag. Try removing ‘#’ from href attribute.And call modal with data attributes.

<button class="btn btn-primary btn-lg" data-toggle="modal" data-target="#myModal">
  Launch demo modal

Solution 3:

$('the thing you click on').click(function ($e) {

This will help you to stop the scroll bar going on the top.It worked for me

Solution 4:

if you are using anchor tag as <a href"#" ..> ... </a> and href="#" is creating problem, remove that.
You can read more over here

Solution 5:

I don’t see a specific error, but I would advise you to check your html syntax.

A tiny test with your source gives me errors like

Line 127, Column 34: Unclosed element div.

              <div class="inner onlySides">

This could be an issue.

Solution 6:

Possibly working with modals nested somewhere in the code:

body.modal-open {
    overflow: visible;
    position: absolute;
    width: 100%;

For me it was a combination of position, height and overflow.