前端吸顶效果

一、原生js的方式:

复制代码
      * {
        margin: 0;
        padding: 0;
      }
      body {
        height: 2000px;
      }
      .header {
        height: 100px;
        background-color: red;
      }
      .nav {
        line-height: 50px;
        background-color: greenyellow;
        text-align: center;
      }
      .nav.fixed {
        position: fixed;
        top: 0;
        width: 100%;
      }
      .container {
        height: 500px;
        background-color: yellow;
      }
      .container.marginTop {
        margin-top: 50px;
      }
复制代码
复制代码
    <div class="header"></div>
    <div class="nav">我是导航栏</div>
    <div class="container">
      <ul>
        <li>1</li>
        <li>2</li>
        <li>3</li>
        <li>4</li>
        <li>5</li>
      </ul>
    </div>
复制代码
复制代码
      const headerHeight = document.querySelector('.header').offsetHeight // header的高度
      const nav = document.querySelector('.nav')
      const container = document.querySelector('.container')
      window.addEventListener('scroll', () => {
        const scrollTop =
          document.documentElement.scrollTop ||
          window.pageYOffset ||
          document.body.scrollTop // 向上卷曲出去的距离
        // 当滚动条向上卷曲出去的距离大于等于header的高度时,给nav添加固定定位,并且给container添加
        if (scrollTop >= headerHeight) {
          nav.classList.add('fixed')
          container.classList.add('marginTop')
        } else {
          nav.classList.remove('fixed')
          container.classList.remove('marginTop')
        }
      })
复制代码

 

 

 

二、vue

复制代码
<template>
  <div id="app">
    <header></header>
    <div class="container">
      <div class="nav">导航</div>
      <ul>
        <li>1</li>
        <li>2</li>
        <li>3</li>
        <li>4</li>
        <li>6</li>
      </ul>
    </div>
  </div>
</template>
复制代码
复制代码
  mounted() {
    this.ceiling()
  },
  methods: {
    ceiling() {
      const headerHeight = document.querySelector('header').offsetHeight + 20 // 20是上下边距加起来20px
      const nav = document.querySelector('.nav')
      const container = document.querySelector('.container')
      window.addEventListener('scroll', () => {
        const scrollTop =
          document.documentElement.scrollTop ||
          window.pageYOffset ||
          document.body.scrollTop
        console.log(scrollTop, headerHeight)
        if (scrollTop >= headerHeight) {
          nav.classList.add('fixed')
          container.classList.add('marginTop')
        } else {
          nav.classList.remove('fixed')
          container.classList.remove('marginTop')
        }
      })
    }
  }
复制代码
复制代码
#app {
  height: 1000px;
  padding: 10px;
  header {
    height: 100px;
    background-color: red;
  }
  .container {
    margin-top: 10px;
    .nav {
      line-height: 40px;
      text-align: center;
      background-color: yellowgreen;
    }
    .nav.fixed {
      position: fixed;
      top: 0;
      width: calc(100% - 20px);
    }
    > ul {
      background-color: yellow;
      height: 500px;
    }
  }
  .container.marginTop {
    margin-top: 50px; // nav高度40 + 原有的margin-top: 10px
  }
}
View Code
复制代码

 

posted @   吴小明-  阅读(287)  评论(0编辑  收藏  举报
编辑推荐:
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
阅读排行:
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· 提示词工程——AI应用必不可少的技术
· 字符编码:从基础到乱码解决
· 地球OL攻略 —— 某应届生求职总结
点击右上角即可分享
微信分享提示