每当浏览器添加滚动条时,内容就会水平跳跃
我使用固定宽度的正文和自动边距将内容居中于页面中间。当内容超出页面高度并且浏览器添加滚动条时,自动边距会强制内容跳动滚动条左侧宽度的一半。
将outerHeight 与window.innerHeight 进行比较是解决此问题的适当方法吗?还有其他方法可以解决这个问题吗?
我认为这应该是解决问题的足够信息,但如果我可以回答其他问题,请告诉我。
编辑以澄清:我不想强制显示滚动条。
I'm using a fixed width body and auto margins to center my content in the middle of the page. When the content exceeds the page's height and the browser adds a scrollbar, the auto margins force the content to jump half the width of the scrollbar left.
Is comparing outerHeight with window.innerHeight an appropriate way of solving this? Is there another way to solve this?
I think this should be enough info for the problem, but let me know if I can answer anything else.
Edit for clarification: I don't want to force the scrollbar to appear.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
我将把这个链接留在这里,因为这对我来说似乎是一个优雅的解决方案:
https:// aykevl.nl/2014/09/fix-jumping-scrollbar
他所做的是添加这个 css:
这会将内容向左移动滚动条的大小,因此当它出现时内容已被移动。这适用于将
overflow: auto;
应用于html
标记的居中内容。媒体查询在手机上禁用了此功能,因为边距宽度的差异非常明显。您可以在此处查看示例:
http://codepen.io/anon/pen/NPgbKP
I'll just leave this link here because it seems an elegant solution to me:
https://aykevl.nl/2014/09/fix-jumping-scrollbar
What he does is add this css:
This will move the content to the left just the size of the scrollbar, so when it appears the content is already moved. This works for centered content with
overflow: auto;
applied to thehtml
tag. The media query disables this for mobile phones, as its very obvious the difference in margin widths.You can see an example here:
http://codepen.io/anon/pen/NPgbKP
使用这个CSS:
Use this CSS:
我自己也遇到过这个问题,并且找到了两种解决方法:
始终强制滚动条存在:
body { 溢出-y:滚动; }
在html
上设置它并不适用于所有浏览器,或者如果滚动条确实出现,可能会出现双滚动条。添加一个类,如果没有滚动条,该类会向页面的右边缘添加约 30 像素。
我选择了选项 1,但我不确定它是否适用于所有浏览器(尤其是较旧的浏览器)。
Facebook 使用选项 2。
I've run into this problem myself and I've found two ways to solve it:
Always force the scrollbar to be present:
body { overflow-y: scroll; }
Setting it on thehtml
doesn't work in all browsers or might give double scroll bars if the scrollbar does appear.Add a class that adds ~30 pixels to the right margin of your page if there is no scrollbar.
I've chosen option 1 but I'm not sure if it works in all browsers (especially the older ones).
Facebook uses option 2.
您可以强制滚动条始终显示:
http://www.mediacollege.com/ internet/css/scroll-always.html
You can force the scrollbar to always appear:
http://www.mediacollege.com/internet/css/scroll-always.html
过程是:
即使不需要任何滚动条,这也会显示滚动条。
The process is :
This will show the scrollbar even there no need any scroll bar.
通过 CSS 的最佳方式,它将相应地显示/隐藏滚动条,将
解决跳转问题,适用于所有浏览器
Best possible way through CSS, It will show/hide Scrollbar accordingly, will
solve jump problem, works on every browser
对我来说,解决方案是将此规则添加到正文中:
此规则是最近添加的,旨在减少浏览器的可变性,这些浏览器对于如何应对溢出有不同的默认假设。例如,Chrome 默认启用溢出锚定,而 Firefox 则没有。设置此属性将强制两个浏览器以相同的方式运行。
https://developer.mozilla.org/en-US/文档/Web/CSS/overflow-anchor
For me, the solution was to add this rule to the body:
This rule was added recently, and aims to reduce the variability of browsers having different default assumptions about how they should react to overflowing. Chrome, for example, has overflow anchoring enabled by default, whereas Firefox does not. Setting this property will force both browsers to behave the same way.
https://developer.mozilla.org/en-US/docs/Web/CSS/overflow-anchor