当前位置:首页 > 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 变化时自动更新激活状态。

vue楼层导航怎么实现

<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 并设置滚动行为。

vue楼层导航怎么实现

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);
}

完整组件示例

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

<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中实现继承可以通过多种方式模拟类继承的效果,以下是常见的几种方法: 使用mixins实现逻辑复用 通过Vue的mixins机制共享组件选项,达到类似继承的效果: const Bas…

vue实现密码框的包

vue实现密码框的包

Vue 密码框实现方案 在 Vue 中实现密码框功能,可以通过原生 HTML 结合 Vue 指令或使用第三方组件库完成。以下是几种常见方案: 原生 HTML + Vue 双向绑定 <templ…

用vue实现滑动输入条

用vue实现滑动输入条

使用 Vue 实现滑动输入条 使用原生 HTML5 input range 实现 在 Vue 模板中直接使用 <input type="range">,通过 v-model 绑定数据:…

vue 实现评分

vue 实现评分

Vue 实现评分功能 在 Vue 中实现评分功能可以通过多种方式完成,包括使用第三方组件库或自定义实现。以下是两种常见的方法: 使用第三方组件库(如 Element UI) Element UI 提…

vue实现menu

vue实现menu

Vue 实现 Menu 的方法 使用 Element UI 的 Menu 组件 Element UI 提供了现成的 Menu 组件,适合快速实现导航菜单。安装 Element UI 后,可以直接使用…

vue实现链接

vue实现链接

Vue 实现链接的方法 在 Vue 中实现链接可以通过多种方式,包括使用 <router-link>、<a> 标签或编程式导航。以下是几种常见的方法: 使用 <rout…