gpt4 book ai didi

css - 当元素的位置属性为粘性时如何设置初始顶部位置

转载 作者:行者123 更新时间:2023-11-28 03:21:37 25 4
gpt4 key购买 nike

我想用 top: 45px 设置 #box4 的初始位置,就像 #box3 相对于 #box2,但仍然保留 #box4position: sticky,这使得它在向下滚动页面后处于同一级别。

初始位置,

enter image description here

滚动后,

enter image description here

html,
body {
margin: 0;
padding: 0;
height: 1000px;
}

.box {
width: 30px;
height: 30px;
border: 1px solid #bdbdbd;
}

#box1 {
position: static;
}

#box2 {
position: relative;
left: 50px;
top: -15px;
}

#box3 {
position: fixed;
top: 30px;
left: 100px;
}

#box4 {
position: sticky;
top: 30px;
left: 150px;
}
<div class="box" id="box1">
B1 Static
</div>

<div class="box" id="box2">
B2 Relative
</div>

<div class="box" id="box3">
B3 Fixed
</div>

<div class="box" id="box4">
B4 Sticky
</div>

最佳答案

一种可能的解决方法是使用负边距顶部将 #box4 定位到正确的位置。

#box4 {
margin: -15px;
position: sticky;
top: 30px;
left: 150px;
}

关于css - 当元素的位置属性为粘性时如何设置初始顶部位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45184615/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com