Setting overflow-y: hidden; causes the page to jump to the top in Firefox

17,960

Solution 1

Don't use overflow: hidden on html, only on body. I had the same problem but fixed it by removing html.

Instead :

$('body, html').css('overflow', 'hidden');

Do :

$('body').css('overflow', 'hidden');

Solution 2

I had the same issue after checking it in the inspector window, I noticed that in the reset CSS, HTML is set to

HTML {
    overflow-y: scroll;
}

you can fix this by setting it to

HTML {
    overflow-y: initial;
}

If you don't want to touch reset CSS or just comment it

plugin and code is absolutely fine

Solution 3

change modal position from absolute to fixed:

#mymodal {
    position: fixed
 }
Share:
17,960
Runcible
Author by

Runcible

Updated on June 03, 2022

Comments

  • Runcible
    Runcible about 2 years

    I've got some javascript which handles opening modal popups on my website, and it also sets the overflow-y property on the <html> element to hidden. In Chrome and IE this works as expected - the scrollbar hides, and the page behind the modal popup remains in the same scroll position. When the popup is closed, overflow-y is set to scroll and the page is in the same state and position as before.

    However in Firefox, as soon as overflow-y is changed to hidden the page scroll position jumps to the very top, and so when the popup is closed the view has changed for the user - not ideal.

    The problem can be seen on this jsfiddle

    Is there any solution for this behaviour?