js实现固钉效果

先添加样式

<style>
      .Affix {
        position: fixed;
        top: 0;
        left: 0;
      }
    </style>

监听滚动和页面距离

    const oAffix = document.getElementById("navigator");
      const offsetT = oAffix.offsetTop;
      window.onscroll = function () {
        const scrollH =
          document.documentElement.scrollTop || document.body.scrollTop;
        scrollH >= offsetT
          ? (oAffix.className = "Affix")
          : (oAffix.className = "");
      };
posted @ 2021-08-12 09:42  松岛川树  阅读(11)  评论(0编辑  收藏  举报