当前位置:首页 > 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 中,路由跳转可以通过 vue-router 实现,以下是几种常见的方法: 声明式导航(模板中使用 <router-link>) 通过 <ro…

php实现页面跳转

php实现页面跳转

PHP 页面跳转方法 在 PHP 中,可以通过多种方式实现页面跳转,以下是几种常见的方法: 使用 header() 函数 header() 函数是 PHP 中最常用的跳转方法,通过发送 HTTP 头…

js实现页面跳转

js实现页面跳转

使用 window.location.href 通过修改 window.location.href 属性实现页面跳转: window.location.href = 'https://example…

vue实现拼接路径实现跳转

vue实现拼接路径实现跳转

在Vue中拼接路径并实现跳转 使用Vue Router的router.push方法结合模板字符串或路径拼接方法 // 在方法中拼接路径 methods: { navigateTo(pat…

vue页面跳转怎么实现

vue页面跳转怎么实现

路由跳转(Vue Router) 在Vue项目中,页面跳转通常通过Vue Router实现。确保已安装并配置Vue Router后,可通过以下方式跳转: 声明式导航 使用<router-lin…

vue按钮实现页面跳转

vue按钮实现页面跳转

Vue 按钮实现页面跳转的方法 在 Vue 中实现按钮跳转页面可以通过多种方式实现,以下是常见的几种方法: 使用 router-link 组件 router-link 是 Vue Router 提供…