吸顶功能

发布于 2022-10-24  24 次阅读


监听 scroll 计算

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <style>
      * {
        padding: 0;
        margin: 0;
      }
      .container {
        width: 100vw;
        height: 100vh;
        box-sizing: border-box;
        padding: 48px 0 50px;
      }
      .header {
        position: fixed;
        top: 0;
        left: 0;
        width: 100%;
        height: 48px;
        background-color: pink;
      }
      .footer {
        position: fixed;
        width: 100%;
        height: 50px;
        left: 0;
        bottom: 0;
        background-color: pink;
      }
      .content {
        position: relative;
        height: 100%;
        overflow-y: scroll;
        box-sizing: border-box;
      }
      /* section h2 {
        position: sticky;
        top: 0;
        background-color: lightyellow;
      } */
      section {
        position: relative;
        box-sizing: border-box;
        padding-top: 40px;
      }
      section h2 {
        display: flex;
        justify-content: center;
        align-items: center;
        position: absolute;
        left: 0;
        top: 0;
        width: 100%;
        height: 40px;
        background-color: lightyellow;
      }
      section h2.sticky {
        position: fixed;
        top: 48px;
      }
    </style>
  </head>
  <body>
    <div class="container">
      <div class="header"></div>
      <div class="content">
        <section>
          <h2>HEADER1</h2>
          <div class="desc">
            ...
          </div>
        </section>
        <section>
          <h2>HEADER2</h2>
          <div class="desc">
            ...
          </div>
        </section>
        <section>
          <h2>HEADER3</h2>
          <div class="desc">
            ...
          </div>
        </section>
      </div>
      <div class="footer"></div>
    </div>
    <script>
      const oContent = document.getElementsByClassName("content")[0];
      const oH2List = document.getElementsByTagName("h2");
      const offsetTopList = [];
      [...oH2List].forEach((ele) => {
        offsetTopList.push({
          offsetTop: ele.parentElement.offsetTop,
          clientHeight: ele.parentElement.clientHeight,
        });
      });
      oContent.onscroll = function (e) {
        offsetTopList.forEach((v, idx) => {
          if (
            oContent.scrollTop > v.offsetTop &&
            oContent.scrollTop < v.clientHeight + v.offsetTop
          ) {
            oH2List[idx].classList.add("sticky");
          } else {
            oH2List[idx].classList.remove("sticky");
          }
        });
      };
    </script>
  </body>
</html>

position: sticky

boxTop {
	position: sticky;
	top: 0;
}
最后更新于 2023-07-20