Sticky div 的奇怪行为
我正在尝试让我的 div 粘性定位。我对 div 定位的奇怪行为感到困惑。根据上面的代码,它按预期工作,但是如果我尝试将粘性 div 移动到具有橙色背景的 div 内,则它不起作用。有人可以帮我解释这种奇怪的行为吗?
div.one {
position: -webkit-sticky;
position: sticky;
top: 0;
background-color: yellow;
padding: 50px;
font-size: 20px;
}
<h2>Sticky Element: Scroll Down to See the Effect</h2>
<p>Scroll down this page to see how sticky positioning works.</p>
<div style="background:red; padding:10px">
<div>
<div style="background:orange">
<p>Just some random text
<p>
//If sticky div (class="one") moved here then doesn't work
</div>
<div class="one">This is sticky</div>
<div class="two" style="background:green;height:1200px">
<h1>Remaining</h1>
</div>
</div>
</div>
I am trying to get my div sticky positioned. I got confused with the strange behaviour basis the positioning of the div. As per the above code, it is working as expected however if I try to shift my sticky div inside div with background orange then it doesn't work. Could someone help me with the explanation for this strange behaviour.
div.one {
position: -webkit-sticky;
position: sticky;
top: 0;
background-color: yellow;
padding: 50px;
font-size: 20px;
}
<h2>Sticky Element: Scroll Down to See the Effect</h2>
<p>Scroll down this page to see how sticky positioning works.</p>
<div style="background:red; padding:10px">
<div>
<div style="background:orange">
<p>Just some random text
<p>
//If sticky div (class="one") moved here then doesn't work
</div>
<div class="one">This is sticky</div>
<div class="two" style="background:green;height:1200px">
<h1>Remaining</h1>
</div>
</div>
</div>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论