我在 chrome 44 中遇到此代码的问题(这里是 fiddle :http://jsfiddle.net/o6b8rdh8/):
<div id="body">
<div id="header">
Header...
</div>
<div id="content">
<div id="main">
<p>Content</p><p>Content</p><p>Content</p><p>Content</p><p>Content</p><p>Content</p><p>Content</p><p>Content</p><p>Content</p><p>Content</p><p>Content</p><p>Content</p><p>Content</p><p>Content</p><p>Content</p><p>Content</p><p>Content</p><p>Content</p><p>Content</p><p>Content</p><p>Content</p><p>Content</p><p>Content</p><p>Content</p><p>Content</p><p>Content</p><p>Content</p>
</div>
<div id="parent">
<div id="background">
</div>
<div id="card">
Lorem...
</div>
</div>
</div>
</div>
#body {
top: 0;
bottom: 0;
right: 0;
left: 0;
position: absolute;
overflow: hidden;
}
#header {
position: fixed;
height: 50px;
background-color: yellow;
opacity: 0.5;
width: 100%;
}
#content {
position: absolute;
top: 50px;
bottom: 0;
right: 0;
left: 0;
overflow: auto;
}
#parent {
position: fixed;
top: 0;
bottom: 0;
right: 0;
left: 0;
overflow: auto;
}
#background {
opacity: 0.5;
position: fixed;
top: 0;
bottom: 0;
right: 0;
left: 0;
background-color: blue;
}
#card {
position: relative;
margin: 50px 50px 0;
background-color: white;
height: 2000px;
border: 5px solid red;
}
以上为问题截图
问题是当我滚动时,固定背景也会滚动。它应该保持固定。
它在 Firefox 39 和 Safari 8 中完美运行。
知道我们该如何解决这个问题吗?
谢谢。
编辑 1: 移动设备上同样的问题。使用 Chrome Latest 在 OnePlus One 和 Nexus 9 上复制。
编辑 2: 我已经更新了 fiddle 以反射(reflect)更多的现实。这打破了@vivekkupadhyay 的答案:(。
这是另一个显示模态视图关闭时用例的 fiddle :http://jsfiddle.net/et0d4dox/1/
初始 fiddle ( http://jsfiddle.net/o6b8rdh8/ ) 是模式打开时的用例。
最佳答案
好的!我找到了答案!
我们需要在#background
的CSS
中添加:-webkit-transform: translateZ(0);
#background
css 现在是:
#background {
opacity: 0.5;
position: fixed;
top: 0;
bottom: 0;
right: 0;
left: 0;
background-color: blue;
-webkit-transform: translateZ(0);
}
更新后的 fiddle 是:http://jsfiddle.net/o6b8rdh8/1/
我不确定为什么需要添加此属性。如果有人对此有答案,我会很高兴知道。
关于html - Bug chrome : parent fixed scroll, 第一个固定的 child (背景色)也滚动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32203310/