【发布时间】:2021-02-23 06:25:12
【问题描述】:
我在启用了overflow:auto 的嵌套弹性容器中遇到了一个粘性元素的奇怪问题。我希望粘性元素与第二个元素具有相同的高度,并且一旦滚动到阈值(top: 0),它也会粘在其包含元素的顶部。
请注意:该行为在 Chrome/Edge/Firefox 中正常,但在 Safari 中则不行。
据此,sticky 应该可以在带有前缀-webkit 的 Safari 中使用。 (https://caniuse.com/?search=sticky)
有什么好的方法可以在 Safari 中完成这项工作吗?
提前致谢。
.wrapper {
height: 100px;
background-color: red;
display: flex;
}
.container {
height: 200px;
display: flex;
overflow: auto;
}
.first {
position: sticky;
position: -webkit-sticky;
top: 0;
background-color: blue;
}
.second {
background-color: yellow;
height: 500px;
}
<div class="wrapper">
<div class="container">
<div class="first">Scroll down
</div>
<div class="second">Here</div>
</div>
</div>
为遇到类似问题并想了解粘性的人提供有用的链接:
【问题讨论】: