9

When I open a modal window it scrolls the background context all the way up to the top. Example I have links on the bottom of the page that open a modal window. When I click on the link the window will scroll up to the top of the document then display the modal window. I have no problem with the positioning of the modal window. I just don't want the scroll up to occur before it opens the page. I'm using mostly the default CSS that came from BS v3 (latest on github) and pretty much the default modal body. I am using JS to populate the label and body of the modal then opening it

<!-- Modal -->
<div class="modal fade" id="modal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
  <div class="modal-dialog">
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
        <h4 class="modal-title" id="myModalLabel">Modal title</h4>
      </div>
      <div class="modal-body" id="myModalBody"></div>
    </div><!-- /.modal-content -->
  </div><!-- /.modal-dialog -->
</div><!-- /.modal -->

4 Answers4

20

I had the same problem. I had a button calling a javascript function to open the modal:

<a href="#" onclick="myFunction(id)">open modal</a>

and changed it to:

<a href="javascript:void(0)" onclick="myFunction(id)">open modal</a>

and this solved the scrolling to top problem.

I am using bootstrap 3.3.7 I believe.

jfsiman
  • 201
  • 2
  • 2
4

I found the problem was some CSS I used to always force a scrollbar. This CSS gave the body a height of 100%. As soon as I removed this the modal worked perfectly without scrolling to top.

Dave Perry
  • 56
  • 2
1

It could be the position of your modal code in the HTML tree, try moving it levels up or down.

JelteF
  • 3,021
  • 2
  • 27
  • 35
1

I found this answer resolves it more broadly: Bootstrap modal: background jumps to top on toggle

Just add the following CSS to your page:

body.modal-open {
    overflow: visible;
}
Rozgonyi
  • 1,027
  • 13
  • 27