当前位置:首页 > VUE

vue实现楼梯效果

2026-01-23 05:00:02VUE

Vue实现楼梯效果

楼梯效果通常指页面滚动时,侧边栏或导航栏根据滚动位置高亮对应的章节标题。以下是实现方法:

使用Intersection Observer API

Intersection Observer API可以监听元素是否进入视口,适合实现楼梯效果。

<template>
  <div>
    <div class="content" v-for="(item, index) in sections" :id="`section-${index}`">
      {{ item }}
    </div>
    <div class="stair-nav">
      <div 
        v-for="(item, index) in sections" 
        :class="{ active: activeIndex === index }"
        @click="scrollTo(index)"
      >
        {{ item }}
      </div>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      sections: ['Section 1', 'Section 2', 'Section 3'],
      activeIndex: 0,
      observer: null
    }
  },
  mounted() {
    this.initObserver()
  },
  methods: {
    initObserver() {
      const options = {
        root: null,
        rootMargin: '0px',
        threshold: 0.5
      }

      this.observer = new IntersectionObserver((entries) => {
        entries.forEach(entry => {
          if (entry.isIntersecting) {
            const id = entry.target.id
            this.activeIndex = parseInt(id.split('-')[1])
          }
        })
      }, options)

      this.sections.forEach((_, index) => {
        const element = document.getElementById(`section-${index}`)
        this.observer.observe(element)
      })
    },
    scrollTo(index) {
      const element = document.getElementById(`section-${index}`)
      element.scrollIntoView({ behavior: 'smooth' })
    }
  },
  beforeDestroy() {
    this.observer.disconnect()
  }
}
</script>

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

使用scroll事件监听

另一种传统方法是监听scroll事件,计算各个section的位置。

<template>
  <!-- 同上 -->
</template>

<script>
export default {
  data() {
    return {
      sections: ['Section 1', 'Section 2', 'Section 3'],
      activeIndex: 0,
      sectionPositions: []
    }
  },
  mounted() {
    this.calculatePositions()
    window.addEventListener('scroll', this.handleScroll)
  },
  methods: {
    calculatePositions() {
      this.sectionPositions = this.sections.map((_, index) => {
        const element = document.getElementById(`section-${index}`)
        return element.offsetTop
      })
    },
    handleScroll() {
      const scrollPosition = window.scrollY + 100

      for (let i = 0; i < this.sectionPositions.length; i++) {
        if (
          scrollPosition >= this.sectionPositions[i] &&
          (i === this.sectionPositions.length - 1 || 
           scrollPosition < this.sectionPositions[i + 1])
        ) {
          this.activeIndex = i
          break
        }
      }
    },
    scrollTo(index) {
      window.scrollTo({
        top: this.sectionPositions[index],
        behavior: 'smooth'
      })
    }
  },
  beforeDestroy() {
    window.removeEventListener('scroll', this.handleScroll)
  }
}
</script>

使用第三方库

vue-scrollactive是一个专门实现此效果的库:

vue实现楼梯效果

import VueScrollactive from 'vue-scrollactive'

Vue.use(VueScrollactive)

<template>
  <div>
    <div v-scrollactive v-for="(item, index) in sections" :id="`section-${index}`">
      {{ item }}
    </div>
    <div class="stair-nav">
      <a 
        v-for="(item, index) in sections" 
        href=`#section-${index}`
        class="scrollactive-item"
      >
        {{ item }}
      </a>
    </div>
  </div>
</template>

注意事项

  • Intersection Observer API性能更好,推荐使用
  • 对于动态内容,需要在内容变化后重新计算位置
  • 移动端需要考虑视口高度和滚动行为差异
  • 添加防抖优化scroll事件性能

以上方法均可实现楼梯导航效果,根据项目需求选择合适方案。Intersection Observer API是现代浏览器推荐的方式,第三方库则提供更简单的集成方案。

标签: 楼梯效果
分享给朋友:

相关文章

vue实现折叠效果

vue实现折叠效果

使用 v-show 或 v-if 实现基础折叠 通过 Vue 的指令 v-show 或 v-if 控制元素的显示与隐藏。v-show 通过 CSS 的 display 属性切换,v-if 直接操作 D…

vue实现检索效果

vue实现检索效果

Vue实现检索功能的方法 在Vue中实现检索功能通常需要结合数据绑定、计算属性和方法。以下是几种常见的实现方式: 基础实现(基于计算属性) <template> <div>…

vue 实现动画效果

vue 实现动画效果

Vue 实现动画效果的方法 Vue 提供了多种方式实现动画效果,包括内置的过渡系统、第三方动画库以及 CSS 动画。以下是几种常见的方法: 使用 Vue 内置过渡系统 Vue 的 <trans…

vue实现taggle效果

vue实现taggle效果

Vue实现Toggle效果 在Vue中实现Toggle效果可以通过多种方式完成,以下是几种常见的方法: 方法1:使用v-model和v-show/v-if 通过v-model绑定一个布尔值,结合v…

vue实现multiselect效果

vue实现multiselect效果

Vue 实现多选(Multiselect)效果 使用第三方库 vue-multiselect 安装 vue-multiselect 库: npm install vue-multiselec…

vue实现回弹效果

vue实现回弹效果

Vue 实现回弹效果的方法 回弹效果(Bounce Effect)常见于移动端交互,可以通过 CSS 动画或 JavaScript 实现。以下是几种在 Vue 中实现回弹效果的方案: 使用 CSS…