当前位置:首页 > VUE

vue楼层导航怎么实现

2026-02-22 07:54:40VUE

Vue 楼层导航实现方法

监听滚动事件与计算当前楼层

通过 window.addEventListener 监听滚动事件,结合 getBoundingClientRect() 计算各楼层容器的位置。使用 Vue 的 ref 获取 DOM 节点,动态判断当前视口所在的楼层区域。

data() {
  return {
    currentFloor: 0,
    floors: [] // 存储各楼层DOM节点
  }
},
mounted() {
  this.floors = this.$refs.floor.map(item => item.$el);
  window.addEventListener('scroll', this.handleScroll);
},
methods: {
  handleScroll() {
    const scrollPosition = window.scrollY;
    this.floors.forEach((floor, index) => {
      const top = floor.offsetTop;
      const height = floor.offsetHeight;
      if (scrollPosition >= top && scrollPosition < top + height) {
        this.currentFloor = index;
      }
    });
  }
}

导航菜单高亮联动

导航菜单使用 v-for 渲染,通过 :class 动态绑定高亮样式。currentFloor 变化时自动更新激活状态。

<div class="nav">
  <div 
    v-for="(item, index) in navItems" 
    :key="index"
    :class="{ active: currentFloor === index }"
    @click="scrollToFloor(index)">
    {{ item.name }}
  </div>
</div>

平滑滚动到指定楼层

通过 scrollTo 或第三方库(如 smooth-scroll)实现点击导航时的平滑滚动效果。计算目标楼层的 offsetTop 并设置滚动行为。

scrollToFloor(index) {
  const target = this.floors[index];
  window.scrollTo({
    top: target.offsetTop,
    behavior: 'smooth'
  });
}

性能优化建议

使用防抖函数(如 lodash.debounce)减少滚动事件触发频率。组件销毁时移除事件监听,避免内存泄漏。

import { debounce } from 'lodash';

created() {
  this.debouncedScroll = debounce(this.handleScroll, 100);
},
beforeDestroy() {
  window.removeEventListener('scroll', this.debouncedScroll);
}

完整组件示例

以下是一个整合后的组件代码示例:

vue楼层导航怎么实现

<template>
  <div>
    <div class="nav">
      <div 
        v-for="(item, index) in navItems" 
        :key="index"
        :class="{ active: currentFloor === index }"
        @click="scrollToFloor(index)">
        {{ item.name }}
      </div>
    </div>

    <div v-for="(item, index) in floorsData" :key="index" ref="floor">
      <h3>{{ item.title }}</h3>
      <p>{{ item.content }}</p>
    </div>
  </div>
</template>

<script>
import { debounce } from 'lodash';

export default {
  data() {
    return {
      currentFloor: 0,
      floors: [],
      navItems: [
        { name: '楼层1' },
        { name: '楼层2' }
      ],
      floorsData: [
        { title: '楼层1标题', content: '...' },
        { title: '楼层2标题', content: '...' }
      ]
    };
  },
  mounted() {
    this.floors = this.$refs.floor;
    window.addEventListener('scroll', this.debouncedScroll);
  },
  created() {
    this.debouncedScroll = debounce(this.handleScroll, 100);
  },
  methods: {
    handleScroll() {
      const scrollPosition = window.scrollY;
      this.floors.forEach((floor, index) => {
        const top = floor.offsetTop;
        const height = floor.offsetHeight;
        if (scrollPosition >= top && scrollPosition < top + height) {
          this.currentFloor = index;
        }
      });
    },
    scrollToFloor(index) {
      const target = this.floors[index];
      window.scrollTo({
        top: target.offsetTop,
        behavior: 'smooth'
      });
    }
  },
  beforeDestroy() {
    window.removeEventListener('scroll', this.debouncedScroll);
  }
};
</script>

<style>
.nav {
  position: fixed;
  right: 20px;
  top: 50%;
  transform: translateY(-50%);
}
.nav div {
  padding: 10px;
  cursor: pointer;
}
.nav div.active {
  color: red;
  font-weight: bold;
}
</style>

标签: 楼层vue
分享给朋友:

相关文章

课程表vue实现

课程表vue实现

实现课程表的Vue组件 创建课程表需要设计数据结构、布局和交互逻辑。以下是一个基于Vue 3的实现方案: 数据结构设计 const timetableData = ref([ { ti…

vue   实现单选

vue 实现单选

Vue 实现单选功能 在 Vue 中实现单选功能可以通过多种方式,以下是几种常见的方法: 使用原生 <input type="radio"> 绑定 v-model 通过 v-model…

vue实现ios

vue实现ios

Vue 实现 iOS 风格应用 使用 Vue 实现 iOS 风格的应用,可以通过结合 UI 框架和自定义样式来达成目标。以下是具体方法和步骤: 选择 iOS 风格的 UI 框架 Vue 生态中有多…

vue实现treeview

vue实现treeview

Vue 实现 TreeView 的方法 使用递归组件实现 TreeView 递归组件是 Vue 中实现 TreeView 的常见方式。通过组件调用自身,可以轻松处理嵌套的树形结构数据。 <te…

vue实现网站

vue实现网站

Vue 实现网站的基本步骤 Vue.js 是一个渐进式 JavaScript 框架,适合构建现代化的单页应用(SPA)或动态网站。以下是使用 Vue 实现网站的基本方法。 初始化 Vue 项目 使用…

vue 实现折叠

vue 实现折叠

Vue 实现折叠功能的方法 在 Vue 中实现折叠功能可以通过多种方式完成,以下是几种常见的实现方法: 使用 v-if 或 v-show 控制显示隐藏 通过绑定一个布尔值变量,结合 v-if 或 v…