Sticky div 的奇怪行为

发布于 2025-01-11 09:34:02 字数 1022 浏览 2 评论 0原文

我正在尝试让我的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文