实现锚点链接点击tab跳转到指定位置 并且滚动鼠标顶部锚点的样式也跟随变化

news/2024/7/7 21:31:43

实现效果如下  不管是点击还是  滚动鼠标 顶部的样式也会跟随变化

点击会跳转到指定的位置 

通过IntersectionObserver 监听是否可见 

下面代码可以直接执行到vue的文件 

<template>
  <div>
    <ul class="nav">
      <li v-for="tab in tabs" :key="tab.name" :class="{ active: currentTab === tab.name }" @click="scrollToTab(tab)">
        {{ tab.label }}
      </li>
      <div class="underline" :style="underlineStyle"></div>
    </ul>
    <div class="section" id="section1">Section 1</div>
    <div class="section" id="section2">Section 2</div>
    <div class="section" id="section3">Section 3</div>
    <div class="section" id="section4">Section 4</div>
    <div class="section" id="section5">Section 5</div>
  </div>
</template>

<script>
export default {
  data () {
    return {
      currentTab: 'tab1',
      tabs: [
        { name: 'tab1', label: '首页', id: 'section1' },
        { name: 'tab2', label: '服务内容', id: 'section2' },
        { name: 'tab3', label: '业务案例', id: 'section3' },
        { name: 'tab4', label: '关于我们', id: 'section4' },
        { name: 'tab5', label: '联系方式', id: 'section5' }
      ],
      underlineStyle: {
        width: '0px',
        left: '0px'
      }
    }
  },
  methods: {
    scrollToTab (section) {
      this.currentTab = section.name
      this.updateUnderline()
      const element = document.getElementById(section.id)
      document.getElementById(section.id).scrollIntoView({ behavior: 'smooth', block: 'center' })
    },
    updateUnderline () {
      this.$nextTick(() => {
        const activeTab = this.$el.querySelector('.nav .active')
        if (activeTab) {
          this.underlineStyle.width = `${activeTab.offsetWidth}px`
          this.underlineStyle.left = `${activeTab.offsetLeft}px`
        }
      })
    },
    handleIntersection (entries) {
      entries.forEach((entry) => {
        if (entry.isIntersecting) {
          const tab = this.tabs.find((tab) => tab.id === entry.target.id)
          if (tab) {
            this.currentTab = tab.name
            this.updateUnderline()
          }
        }
      })
    }
  },
  mounted () {
    this.updateUnderline()
    const options = {
      root: null,
      rootMargin: `-${this.$el.querySelector('.nav').offsetHeight}px 0px 0px 0px`,
      threshold: 0.5
    }

    const observer = new IntersectionObserver(this.handleIntersection, options)
    this.tabs.forEach((tab) => {
      const section = document.getElementById(tab.id)
      if (section) {
        observer.observe(section)
      }
    })

    window.addEventListener('resize', this.updateUnderline)
  },
  beforeDestroy () {
    window.removeEventListener('resize', this.updateUnderline)
  }
}
</script>

<style scoped>
.nav {
  display: flex;
  position: fixed;
  top: 0;
  width: 1000px;
  background-color: white;
  z-index: 1000;
  border-bottom: 1px solid #ccc;
}
.nav li {
  flex: 1;
  text-align: center;
  padding: 10px;
  cursor: pointer;
  position: relative;
}
.nav li.active {
  color: blue;
}
.underline {
  position: absolute;
  bottom: 0;
  height: 2px;
  background-color: blue;
  transition: width 0.3s, left 0.3s;
}
.section {
  height: 300px;
  padding-top: 60px; /* 留出导航栏的高度 */
  border-bottom: 1px solid #ccc;
}
</style>


http://lihuaxi.xjx100.cn/news/2219847.html

相关文章

Vue实现无限滚动加载更多内容(懒加载)或实现查看更多按钮

在Vue中实现无限滚动加载更多内容&#xff0c;通常可以使用vue-infinite-loading插件。以下是一个简单的例子&#xff1a; 1、首先&#xff0c;安装vue-infinite-loading&#xff1a; npm install vue-infinite-loading --save2、在Vue组件中使用它&#xff1a; <templat…

【AICFD教程】汽车外气动仿真,小白学CFD的入门案例

【视频教程】 【教程】汽车外气动仿真&#xff0c;小白学CFD的入门案例 【文字教程】 1. 案例背景 1.1 学习目标 本案例针对某汽车仿真模型&#xff0c;在车速为40m/s时进行了汽车外流场的数值模拟。 本案例教程旨在演示AICFD中以下场景与功能的操作&#xff1a; a. 单域外…

日语 9 10

9. トラブル trouble 荷物 荷物 荷物 荷物 荷物 にもつ 行李 同僚 同僚 同僚 同僚 同僚 どうりょう 同事 出 黒 申し訳 で  くろ  もうしわけ 入国 入国 入国 入国 入国 にゅうこく 入境 相通 相通 相通 相通 相通 あいつう 若者 若者 若者 若…

MySQl基础入门⑯【操作视图】完结

上一边文章内容 表准备 CREATE TABLE Students (id INT AUTO_INCREMENT PRIMARY KEY,name VARCHAR(100),email VARCHAR(255),major VARCHAR(100),score int,phone_number VARCHAR(20),entry_year INT,salary DECIMAL(10, 2) );数据准备 INSERT INTO Students (id, name, ema…

视频太大了怎么缩小内存

我们在分享视频的时候&#xff0c;有时候会遇到过视频文件太大&#xff0c;无法发送或者上传的情况&#xff0c;别担心&#xff0c;今天我就来给大家分享一个简单有效的方法&#xff0c;让你的视频变得更小&#xff0c;更方便分享&#xff01; 打开 “51视频处理官网 的网站。上…

前端开发之TCP与UDP

上一篇&#x1f449;: 前端开发之性能优化 TCP与UDP 三次握手 1. 初始状态&#xff1a; 客户端开始时处于CLOSED状态&#xff0c;表明没有活动的连接。服务器监听特定端口&#xff0c;处于LISTEN状态&#xff0c;等待连接请求。 2. 第一次握手&#xff08;SYN_SENT状态&am…

工程项目全生命周期管理系统企智汇一站式解决方案!

在当今竞争激烈的工程行业&#xff0c;实现工程项目的全生命周期管理已成为企业提升效率、降低成本、确保质量并赢得市场竞争的关键。企智汇的工程项目管理系统解决方案致力于助力工程企业实现全面、精细化的项目全生命周期管理&#xff0c;确保从项目启动到竣工交付的每一个环…

几个小实验

小实验 shh远程管理 ssh是一种安全通道协议&#xff0c;只能用来实现字符界面的远程登录。远程复制&#xff0c;远程文本传输。 ssh对通信双方的数据进行了加密。 用户名和密码登录 密钥对认证方式&#xff08;可以实现免密登录&#xff09; ssh 22 网络层 传输层 数据传…