当前位置:首页 > VUE

vue实现楼梯效果

2026-01-23 05:00:02VUE

Vue实现楼梯效果

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

vue实现楼梯效果

使用Intersection Observer API

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

vue实现楼梯效果

<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是一个专门实现此效果的库:

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是现代浏览器推荐的方式,第三方库则提供更简单的集成方案。

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

相关文章

h5页面滑动效果实现

h5页面滑动效果实现

实现H5页面滑动效果的方法 H5页面滑动效果可以通过多种方式实现,包括原生JavaScript、CSS动画或第三方库。以下是几种常见的方法: 使用CSS Scroll Snap CSS Scroll…

vue实现凤凰效果

vue实现凤凰效果

Vue 实现凤凰效果 凤凰效果通常指一种视觉动画,模拟凤凰展翅、火焰燃烧等动态效果。在 Vue 中实现这种效果,可以结合 CSS 动画、Canvas 或第三方动画库(如 GSAP)。以下是几种实现方式…

vue实现烟花效果

vue实现烟花效果

Vue 实现烟花效果的方法 在Vue中实现烟花效果可以通过结合Canvas绘图和动画技术来完成。以下是一种常见的实现方式: 创建Vue组件 新建一个名为Fireworks.vue的组件,核心逻辑如下…

vue实现筛选效果

vue实现筛选效果

Vue 实现筛选效果的方法 基于计算属性的筛选 在 Vue 中可以通过计算属性实现数据的动态筛选。定义一个计算属性,根据用户输入的筛选条件返回过滤后的数据列表。 <template>…

vue实现联动效果

vue实现联动效果

Vue 实现联动效果的方法 联动效果通常指一个组件的值变化时,另一个组件的值或状态随之变化。以下是几种常见的实现方式: 使用 v-model 和计算属性 通过 v-model 绑定数据,结合计算属性…

vue实现数字翻页效果

vue实现数字翻页效果

Vue 实现数字翻页效果 在 Vue 中实现数字翻页效果可以通过多种方式完成,以下是一种常见的实现方法: 使用 transition 和动态绑定 通过 Vue 的 transition 组件和动态绑…