在IT技术中已知一个“bug”是如果容器被翻译,则具有固定位置的元素会失去它们的位置。例如,如果我有这样的结构:
<div class="container">
<div class="fixed"></div>
</div>
假设容器被滚动,当容器发生变换(例如translate(x,y), rotate()等),那么固定元素的行为就像它是相对定位的,并且随着容器一起滚动。例如在最新版本的Firefox浏览器中可以看到这种情况。
有什么方法可以解决这种问题吗?
document.addEventListener('click', function() {
document.getElementById('container').classList.toggle('transformed')
}, false);
#bg {
border: 1px solid #AFA;
height: 100%;
width: 100%;
position: fixed;
top: 0;
left: 0;
}
#container {
border: 1px solid #FAF;
height: 50%;
width: 75%;
position: relative;
margin: 0 auto;
overflow: auto;
}
#content {
background: rgba(125, 175, 0, .7);
position: fixed;
width: 100%;
top: 0;
left: 0;
}
.transformed {
transform: translate(0, 5em);
}
<div id="bg">
<div id="container" class="transformed">
.<br>.<br>.<br>.<br>.<br>.<br>.
this is a scrollable paragraph
<br>.<br>the "fixed" content does scroll with the paragraph
<br>.<br>.<br>.<br>.<br>.<br>.<br>.<br>.<br>.<br>.
you can click to toggle the transformation On/Off
<br>.<br>.<br>.<br>.<br>.
<span id="content">relatively fixed content</span>
</div>
</div>
如果您添加一个包装器,设置其height: 100%; width: 100%;
和overflow: auto
,那么您的"固定"内容将不会随容器滚动。
实际上,现在滚动的不再是您的容器,而是包装器。因此,您可能需要设置容器的overflow: visible
或hidden
,以避免并不是很好的“固定”元素出现意外滚动。
另外,请注意,您的包装器需要是block
或inline-block
元素。
#bg {
border: 1px solid #AFA;
height: 100%;
width: 100%;
position: fixed;
top: 0;
left: 0;
}
#container {
border: 1px solid #FAF;
height: 50%;
width: 75%;
position: relative;
margin: 0 auto;
overflow: visible;
}
#wrapper {
height: 100%;
width: 100%;
overflow: auto;
}
#content {
background: rgba(125, 175, 0, .7);
position: fixed;
width: 100%;
top: 0;
left: 0;
}
.transformed {
transform: translate(0, 50%);
}
<div id="bg">
<div id="container" class="transformed">
<div id="wrapper">
.<br>.<br>.<br>.<br>.<br>.<br>.<br>.<br>.<br>.<br>.<br>.<br>.<br>.<br>.<br>.<br>.<br>.<br>.<br>.<br>.
<span id="content">relatively fixed content</span>
</div>
</div>
</div>
我不熟悉这个错误,但当您使用 positioned: fixed;
时,元素的定位是相对于浏览器窗口的,因此将其放置在容器内没有任何意义。
以下标记是我建议采用的:
<div class="fixed"></div>
<div class="container"></div>
当你在任何元素上使用position: fixed;
时,它会相对于视口定位。直接来自MDN关于position属性的页面。
fixed
不为该元素留出空间。相反,将其定位到屏幕视口的指定位置,并在滚动时不移动它。
所以你正在经历的是实际上应该工作的方式,而不是一个“错误”。
现在,如果你想要的是与.container
div相关联并随之移动的东西,那么你将不得不在这里使用absolute
定位。看一下这个fiddle。重要的CSS是-
.container {
width: 200px;
height: 100px;
position: relative;
}
.absolute {
position: absolute;
width: 20px;
height: 10px;
top: 50px;
left: 50px;
}
absolute
时,我还将外部 div 定位为 relative
,因为内部 div 的位置是相对于最近的父 div 定位为除 static
以外的任何其他值。fixed
,父容器的任何操作都不会起作用。 - vabhdman