当前位置:首页 > VUE

vue实现导航联动

2026-01-18 19:46:22VUE

实现导航联动的基本思路

导航联动通常指在页面滚动时,导航菜单高亮显示当前可见区域的对应项,或点击导航菜单时页面滚动到对应区域。Vue中可通过监听滚动事件或使用第三方库实现。

监听滚动事件实现联动

通过window.addEventListener监听滚动事件,结合Element.getBoundingClientRect()判断当前视口位置与各区块的关系,动态更新导航高亮状态。

vue实现导航联动

<template>
  <div>
    <nav>
      <ul>
        <li v-for="(item, index) in sections" :key="index" 
            :class="{ active: currentIndex === index }"
            @click="scrollTo(index)">
          {{ item.title }}
        </li>
      </ul>
    </nav>
    <section v-for="(item, index) in sections" :key="index" :ref="`section-${index}`">
      <h2>{{ item.title }}</h2>
      <p>{{ item.content }}</p>
    </section>
  </div>
</template>

<script>
export default {
  data() {
    return {
      sections: [
        { title: 'Section 1', content: '...' },
        { title: 'Section 2', content: '...' },
        { title: 'Section 3', content: '...' }
      ],
      currentIndex: 0
    }
  },
  mounted() {
    window.addEventListener('scroll', this.handleScroll)
  },
  beforeDestroy() {
    window.removeEventListener('scroll', this.handleScroll)
  },
  methods: {
    handleScroll() {
      const scrollPosition = window.scrollY
      this.sections.forEach((_, index) => {
        const el = this.$refs[`section-${index}`][0]
        if (el) {
          const { top, bottom } = el.getBoundingClientRect()
          if (top <= 100 && bottom >= 100) {
            this.currentIndex = index
          }
        }
      })
    },
    scrollTo(index) {
      const el = this.$refs[`section-${index}`][0]
      if (el) {
        window.scrollTo({
          top: el.offsetTop,
          behavior: 'smooth'
        })
      }
    }
  }
}
</script>

<style>
.active {
  color: red;
  font-weight: bold;
}
</style>

使用vue-scrollto插件简化实现

安装vue-scrollto插件可快速实现平滑滚动和导航联动功能:

npm install vue-scrollto

配置插件后直接调用方法:

vue实现导航联动

<template>
  <div>
    <nav>
      <ul>
        <li v-for="(item, index) in sections" :key="index" 
            :class="{ active: currentIndex === index }"
            @click="$scrollTo(`#section-${index}`)">
          {{ item.title }}
        </li>
      </ul>
    </nav>
    <section v-for="(item, index) in sections" :key="index" :id="`section-${index}`">
      <h2>{{ item.title }}</h2>
      <p>{{ item.content }}</p>
    </section>
  </div>
</template>

<script>
import VueScrollTo from 'vue-scrollto'
export default {
  data() {
    return {
      sections: [
        { title: 'Section 1', content: '...' },
        { title: 'Section 2', content: '...' },
        { title: 'Section 3', content: '...' }
      ],
      currentIndex: 0
    }
  },
  mounted() {
    window.addEventListener('scroll', this.handleScroll)
  },
  methods: {
    handleScroll() {
      const scrollPosition = window.scrollY
      this.sections.forEach((_, index) => {
        const el = document.getElementById(`section-${index}`)
        if (el) {
          const { top, bottom } = el.getBoundingClientRect()
          if (top <= 100 && bottom >= 100) {
            this.currentIndex = index
          }
        }
      })
    }
  }
}
</script>

性能优化建议

滚动事件可能频繁触发,需要添加节流函数控制执行频率:

methods: {
  handleScroll: _.throttle(function() {
    // 原有逻辑
  }, 100)
}

使用Intersection Observer API替代滚动事件监听,更适合现代浏览器:

mounted() {
  const observer = new IntersectionObserver((entries) => {
    entries.forEach(entry => {
      if (entry.isIntersecting) {
        const index = this.sections.findIndex(
          (_, i) => entry.target.id === `section-${i}`
        )
        if (index !== -1) this.currentIndex = index
      }
    })
  }, { threshold: 0.5 })

  this.sections.forEach((_, index) => {
    const el = document.getElementById(`section-${index}`)
    if (el) observer.observe(el)
  })
}

标签: vue
分享给朋友:

相关文章

vue实现$.extend

vue实现$.extend

Vue 实现类似 jQuery 的 $.extend 功能 在 Vue 中实现类似 jQuery 的 $.extend 功能,可以通过多种方式完成。$.extend 主要用于合并多个对象的属性,Vue…

vue实现erp

vue实现erp

Vue实现ERP系统的关键步骤 技术选型与架构设计 采用Vue 3 + TypeScript构建前端,搭配Pinia状态管理,Element Plus或Ant Design Vue作为UI框架。后端可…

vue实现表白

vue实现表白

Vue 实现表白页面 使用 Vue 可以快速创建一个动态、交互式的表白页面。以下是一个简单的实现方案: 基础结构 创建一个 Vue 项目或单文件组件,包含以下核心部分: <template&…

vue实现fullpage

vue实现fullpage

Vue 实现 FullPage 效果 在 Vue 中实现全屏滚动(FullPage)效果可以通过多种方式完成,以下是几种常见的方法: 使用现有库 vue-fullpage.js 安装 vue-ful…

vue 实现excel

vue 实现excel

Vue 实现 Excel 功能 前端导出 Excel 使用 xlsx 库可以轻松实现前端 Excel 导出功能。安装依赖: npm install xlsx 导出 Excel 示例代码: impo…

vue实现注册

vue实现注册

Vue 实现注册功能 注册功能通常涉及前端表单、数据验证、与后端 API 的交互等步骤。以下是一个完整的 Vue 实现注册功能的方案。 创建注册表单 使用 Vue 的模板语法创建注册表单,包含用户名…