I using twitter bootstrap modals on a one pager website. When I click on the button to open the modal, the background jumps to the top of the page and opens the modal. The same thing happens when you click the close buttons inside the modal.
I tried adding the code below to my CSS file. This stop the background from jumping to the top, however, I can now scroll through the background with the modal opened which I don’t want as well. And also when my modal have overflows, I can see both the scroll bars for the modal and background.
body.modal-open {
overflow: visible;
}
Is there another way to solve this jumping problem without enabling the user to scroll through the background while the modal is opened?
7
Answers
Add following class in css:
add it to body tag when modal is opened and remove it when modal is closed.
You can use the following code and it should work fine:
Assign the above id to any button or link and your model should load up just fine without any issues.
If you have the
height
of eitherbody
orhtml
is set to100%
as in my case, add the following to your CSS file:The background now should keep its original position.
As I also struggled for days with this problem, my solution was simple – I played with the HTML and I found that when the modal was closing .modal-backdrop was causing the issue. So I simply commented out this from my CSS and everything is fine now. Use your Browser inspector for problems like this!
In case this helps anyone for this exact scenario, the following CSS solved the issue:
The first block allows the page scrollbar to show and prevents the jump to the top when opening the modal. The second block stops the modal overflow adding its own second scrollbar.
I had the same issue and removing
href="#"
solved it.This is because modal-open is a class added to body, when modal is opened. Make sure it’s position is not fixed , If it’s fixed make it inherit, that will help you to stay in the same place when popup is opened