且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

滚动页面时停留在页面底部的粘性div

更新时间: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; 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.