当前位置:首页 > VUE

vue实现左右楼层跳转

2026-01-20 01:22:09VUE

实现思路

通过监听滚动事件,计算各楼层元素的位置信息,实现点击左侧导航跳转对应楼层,同时滚动时自动高亮当前所在楼层的导航项。

核心代码实现

<template>
  <div class="container">
    <div class="left-nav">
      <ul>
        <li 
          v-for="(item, index) in floors" 
          :key="index"
          :class="{ active: currentFloor === index }"
          @click="scrollToFloor(index)"
        >
          {{ item.name }}
        </li>
      </ul>
    </div>
    <div class="right-content">
      <div 
        v-for="(item, index) in floors" 
        :key="index"
        :ref="'floor' + index"
        class="floor-item"
      >
        {{ item.content }}
      </div>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      floors: [
        { name: '楼层1', content: '内容1' },
        { name: '楼层2', content: '内容2' },
        { name: '楼层3', content: '内容3' }
      ],
      currentFloor: 0,
      floorPositions: []
    }
  },
  mounted() {
    this.calcFloorPositions()
    window.addEventListener('scroll', this.handleScroll)
  },
  beforeDestroy() {
    window.removeEventListener('scroll', this.handleScroll)
  },
  methods: {
    calcFloorPositions() {
      this.floorPositions = this.floors.map((_, index) => {
        return this.$refs['floor' + index][0].offsetTop
      })
    },
    scrollToFloor(index) {
      window.scrollTo({
        top: this.floorPositions[index],
        behavior: 'smooth'
      })
    },
    handleScroll() {
      const scrollPosition = window.scrollY + 100
      for (let i = 0; i < this.floorPositions.length; i++) {
        if (
          scrollPosition >= this.floorPositions[i] &&
          (i === this.floorPositions.length - 1 || 
           scrollPosition < this.floorPositions[i + 1])
        ) {
          this.currentFloor = i
          break
        }
      }
    }
  }
}
</script>

<style>
.container {
  display: flex;
}
.left-nav {
  position: fixed;
  width: 200px;
}
.right-content {
  margin-left: 220px;
  width: calc(100% - 220px);
}
.floor-item {
  height: 800px;
  margin-bottom: 20px;
  background: #f5f5f5;
}
.active {
  color: red;
  font-weight: bold;
}
</style>

实现要点

  1. 数据结构设计
    使用floors数组存储各楼层信息,包含名称和内容。

    vue实现左右楼层跳转

  2. 位置计算
    在mounted钩子中调用calcFloorPositions方法,通过ref获取各楼层DOM元素的offsetTop值。

  3. 滚动监听
    添加scroll事件监听器,实时计算当前滚动位置对应的楼层索引。

    vue实现左右楼层跳转

  4. 平滑滚动
    使用window.scrollTo方法实现平滑滚动效果,通过behavior: 'smooth'参数。

  5. 响应式样式
    通过currentFloor数据绑定动态设置导航项的高亮样式。

优化建议

  1. 添加防抖处理优化scroll事件性能
  2. 考虑使用Intersection Observer API替代scroll事件监听
  3. 对于动态内容,需要在内容更新后重新计算楼层位置
  4. 移动端适配时可考虑将导航改为横向滚动式

标签: 跳转楼层
分享给朋友:

相关文章

vue实现界面跳转

vue实现界面跳转

路由配置 在Vue项目中实现界面跳转通常依赖Vue Router。需在router/index.js中配置路由路径和组件映射关系: import { createRouter, createWebH…

php实现页面跳转

php实现页面跳转

PHP实现页面跳转的方法 在PHP中,可以通过多种方式实现页面跳转,以下是几种常用的方法: header函数跳转 header("Location: target_page.php"); exit…

js实现跳转

js实现跳转

使用 window.location 跳转 通过修改 window.location.href 或直接使用 window.location 实现页面跳转,适用于普通跳转或带参数的 URL。 //…

js实现跳转

js实现跳转

使用 window.location 跳转 通过修改 window.location.href 实现页面跳转,这是最常用的方法。 window.location.href = 'https:/…

h5实现登录页面跳转页面跳转页面跳转

h5实现登录页面跳转页面跳转页面跳转

使用HTML5实现登录页面跳转 在HTML5中实现登录页面跳转可以通过多种方式完成,包括表单提交、JavaScript跳转和超链接跳转。以下是几种常见的实现方法。 表单提交跳转 使用HTML表单的…

h5实现登录页面跳转

h5实现登录页面跳转

实现登录页面跳转的基本方法 使用HTML5和JavaScript实现登录页面跳转,可以通过表单提交或JavaScript事件触发。以下是几种常见方式: 表单提交跳转 在HTML中创建一个表单,通过a…