position: fixed;样式默认父元素为window窗口,自定义选定父元素方法
不生效举例:此时你会发现父元素只写了一个 position: relative;此时,你会发现,子元素自动把window窗口作为参照对象定位了
<div class="wrap">
<div class="inner"></div>
</div>
.wrap {
width: 500px;
height: 500px;
margin: 200px auto;
border: 1px solid #000;
position: relative;
}
.inner {
width: 100px;
height: 100px;
background: red;
position: fixed;
left: 200px;
top: 0;
}
生效举例父元素增加transform: translate(0,0);此时,你会发现,子元素自动把父元素作为参照对象定位了
<div class="wrap">
<div class="inner"></div>
</div>
.wrap {
width: 500px;
height: 500px;
margin: 200px auto;
border: 1px solid #000;
position: relative;
transform: translate(0,0);
}
.inner {
width: 100px;
height: 100px;
background: red;
position: fixed;
left: 200px;
top: 0;
}
转载自:https://segmentfault.com/a/1190000041994191