当前位置:首页 > VUE

vue实现左右楼层跳转

2026-02-20 17:24:29VUE

实现思路

通过监听滚动事件获取当前滚动位置,与预设的楼层位置进行对比,确定当前所在楼层。点击右侧导航栏时,平滑滚动到对应楼层。

HTML结构

<template>
  <div class="container">
    <!-- 左侧内容区 -->
    <div class="content" ref="content">
      <div v-for="(item, index) in floors" :key="index" :id="`floor-${index}`" class="floor">
        <h2>{{ item.title }}</h2>
        <p>{{ item.content }}</p>
      </div>
    </div>

    <!-- 右侧导航栏 -->
    <div class="nav">
      <ul>
        <li 
          v-for="(item, index) in floors" 
          :key="index"
          :class="{ active: currentFloor === index }"
          @click="scrollTo(index)"
        >
          {{ item.title }}
        </li>
      </ul>
    </div>
  </div>
</template>

JavaScript逻辑

<script>
export default {
  data() {
    return {
      floors: [
        { title: '楼层1', content: '内容1...' },
        { title: '楼层2', content: '内容2...' },
        { title: '楼层3', content: '内容3...' }
      ],
      currentFloor: 0,
      floorPositions: []
    }
  },
  mounted() {
    this.calculatePositions()
    window.addEventListener('scroll', this.handleScroll)
  },
  beforeDestroy() {
    window.removeEventListener('scroll', this.handleScroll)
  },
  methods: {
    calculatePositions() {
      this.floorPositions = this.floors.map((_, index) => {
        const el = document.getElementById(`floor-${index}`)
        return el.offsetTop
      })
    },
    handleScroll() {
      const scrollTop = document.documentElement.scrollTop || document.body.scrollTop
      this.floorPositions.forEach((pos, index) => {
        if (scrollTop >= pos - 100) {
          this.currentFloor = index
        }
      })
    },
    scrollTo(index) {
      const target = this.floorPositions[index]
      window.scrollTo({
        top: target,
        behavior: 'smooth'
      })
    }
  }
}
</script>

CSS样式

<style scoped>
.container {
  display: flex;
}
.content {
  flex: 1;
}
.floor {
  height: 100vh;
  padding: 20px;
  border-bottom: 1px solid #eee;
}
.nav {
  width: 200px;
  position: fixed;
  right: 0;
  top: 50%;
  transform: translateY(-50%);
}
.nav li {
  padding: 10px;
  cursor: pointer;
}
.nav li.active {
  color: red;
  font-weight: bold;
}
</style>

关键点说明

楼层位置计算在mounted钩子中完成,通过获取每个楼层元素的offsetTop值存储到数组。滚动事件监听实时比较滚动位置与楼层位置,更新当前楼层索引。点击导航时使用scrollTo方法实现平滑滚动。

vue实现左右楼层跳转

优化建议

添加防抖处理减少scroll事件触发频率,提升性能。对于动态内容,可在内容变化后调用calculatePositions重新计算位置。考虑使用Intersection Observer API替代scroll事件监听,实现更高效的楼层检测。

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

相关文章

jquery 跳转

jquery 跳转

jQuery 页面跳转方法 使用 jQuery 实现页面跳转可以通过多种方式完成,以下是几种常见的方法: 修改 window.location 属性 // 直接跳转到指定 URL window.l…

vue 实现页面跳转

vue 实现页面跳转

Vue 实现页面跳转的方法 在 Vue 中实现页面跳转可以通过多种方式,主要包括使用 Vue Router 提供的导航方法和原生 JavaScript 的方式。 使用 Vue Router 的 ro…

vue实现多次跳转

vue实现多次跳转

Vue 实现多次跳转的方法 在 Vue 中实现多次跳转可以通过多种方式完成,以下是几种常见的方法: 使用 router.push 或 router.replace 通过 Vue Router 提供…

vue实现登录跳转

vue实现登录跳转

Vue 登录跳转实现方法 使用 Vue Router 进行路由跳转 在 Vue 项目中,可以通过 Vue Router 实现登录后的页面跳转。确保项目中已安装并配置了 Vue Router。 //…

vue按钮怎么实现跳转

vue按钮怎么实现跳转

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

用vue实现页面跳转

用vue实现页面跳转

路由配置 在Vue项目中,页面跳转通常通过Vue Router实现。需要先安装并配置路由。在router/index.js中定义路由规则: import { createRouter, create…