更新时间:2023-01-28 12:01:21
在字面上有一个称为粘性的位置值CSS,不需要jQuery:)
There is literally a position value called 'sticky' in CSS, no need for jQuery :)
这是一个代码段(请参见演示)
Here's a code snippet (see demo)
<!DOCTYPE html>
<style>
body {
height: 600vh;
margin: 0;
}
#sticky {
position: sticky;
top: 90vh;
left: 0px;
width: 100%;
height: 10vh;
background-color: red;
}
#space {
height: 200vh;
}
#container {
height: 200vh;
}
</style>
<div id="space"></div>
<div id="sticky"></div>
粘性 ÷ div>
将在空格 ÷ $ c $之后紧靠相对位置(即没有
位置:粘性
的位置) c>直到用户滚动时,它达到 top
中指定的偏移量,即 90vh
。之后,它将切换到该位置的位置:固定
。
The "sticky" <div>
will be relatively positioned (i.e. positioned as it would be without position: sticky
) right after the "space" <div>
until, as the user scrolls, it reaches the offset specified in top
, that is 90vh
. After that, it'll switch to be position: fixed
at that location.