javascript - 设置溢出-y : hidden; causes the page to jump to the top in Firefox

标签 javascript css overflow

我有一些 javascript 可以处理在我的网站上打开模式弹出窗口,它还设置了 overflow-y <html> 上的属性(property)元素到 hidden .在 Chrome 和 IE 中,这按预期工作 - 滚动条隐藏,模式弹出窗口后面的页面保持在相同的滚动位置。当弹出窗口关闭时,overflow-y设置为 scroll并且页面处于与之前相同的状态和位置。

但是在 Firefox 中,只要 overflow-y更改为 hidden页面滚动位置跳到最顶部,因此当弹出窗口关闭时, View 对用户来说已经改变 - 不理想。

问题可见on this jsfiddle

是否有针对此行为的解决方案?

最佳答案

不要在 html 上使用 overflow: hidden,只能在 body 上使用。 我遇到了同样的问题,但通过删除 html 修复了它。

相反:

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

做:

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

关于javascript - 设置溢出-y : hidden; causes the page to jump to the top in Firefox,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15019510/

相关文章:

r-markdown - 目录 Rmarkdown 中溢出文本的省略号或缩进

使用 gcc 捕获类型转换溢出

css - Jekyll 和自定义 CSS

html - 奇怪的定位顶杆 zurb foundation 5

javascript - 确定函数在闭包 javascript 中是如何被调用的

javascript - 如何删除 firebase 中的整个 Node ?

html - CSS 样式标题 - 背景必须仅覆盖带有底部边框的文本

jquery - 高度变化时创建可滚动的 overflow-y?

javascript - $parsers-array 中的函数未在自定义指令中调用

javascript - 无法使用 jquery ajax 从 json 获取数据