我有一个非常嵌套的<div>元素。如何在此元素中呈现一个将浮动在屏幕顶部(body或html)的新元素?
粘性位置相对于最近的元素(父元素),这不是一个答案。
如果不能用CSS完成,是否有任何JS库可以这样做?
<html>
<body>
<div>
<div>element should be sticky to the top of the page</div>
</div>
</body>
</html>发布于 2022-06-20 20:31:17
向div中添加一个类,并给出固定位置和顶部0,以获得屏幕顶部的元素。
另外,在这个例子中,我给了body 200 the,以便滚动并查看固定在顶部的元素。
body {
height: 200vh;
}
.sticky {
position: fixed;
top: 0;
}<html>
<body>
<div>
<div class="sticky">element should be sticky to the top of the page</div>
</div>
</body>
</html>
https://stackoverflow.com/questions/72691972
复制相似问题