当前位置:首页 > VUE

vue实现fullpage

2026-01-07 20:25:36VUE

Vue 实现 FullPage 效果

在 Vue 中实现全屏滚动(FullPage)效果可以通过多种方式完成,以下是几种常见的方法:

使用现有库 vue-fullpage.js

安装 vue-fullpage.js 库:

npm install vue-fullpage.js --save

在 Vue 项目中引入并使用:

import Vue from 'vue'
import VueFullPage from 'vue-fullpage.js'

Vue.use(VueFullPage)

在组件中使用:

<template>
  <full-page :options="options" ref="fullpage">
    <div class="section">第一屏内容</div>
    <div class="section">第二屏内容</div>
    <div class="section">第三屏内容</div>
  </full-page>
</template>

<script>
export default {
  data() {
    return {
      options: {
        licenseKey: 'YOUR_KEY',
        scrollingSpeed: 700,
        navigation: true,
        anchors: ['page1', 'page2', 'page3']
      }
    }
  }
}
</script>

<style>
.section {
  text-align: center;
  font-size: 3em;
  color: #fff;
}
</style>

使用原生 CSS 和 JavaScript 实现

通过 CSS 和 JavaScript 可以实现基本的全屏滚动效果:

<template>
  <div class="fullpage-container" ref="container">
    <div class="section" v-for="(item, index) in sections" :key="index">
      {{ item.content }}
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      sections: [
        { content: '第一屏内容' },
        { content: '第二屏内容' },
        { content: '第三屏内容' }
      ],
      currentIndex: 0,
      isScrolling: false
    }
  },
  mounted() {
    window.addEventListener('wheel', this.handleScroll, { passive: false })
    this.setSectionHeight()
    window.addEventListener('resize', this.setSectionHeight)
  },
  beforeDestroy() {
    window.removeEventListener('wheel', this.handleScroll)
    window.removeEventListener('resize', this.setSectionHeight)
  },
  methods: {
    setSectionHeight() {
      const height = window.innerHeight + 'px'
      document.querySelectorAll('.section').forEach(el => {
        el.style.height = height
      })
    },
    handleScroll(e) {
      if (this.isScrolling) return
      this.isScrolling = true

      if (e.deltaY > 0 && this.currentIndex < this.sections.length - 1) {
        this.currentIndex++
      } else if (e.deltaY < 0 && this.currentIndex > 0) {
        this.currentIndex--
      }

      this.$refs.container.style.transform = `translateY(-${this.currentIndex * 100}vh)`

      setTimeout(() => {
        this.isScrolling = false
      }, 800)
    }
  }
}
</script>

<style>
.fullpage-container {
  transition: transform 0.8s ease;
}
.section {
  height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
  font-size: 2em;
  color: #fff;
}
</style>

使用 CSS scroll-snap 属性

现代浏览器支持 CSS scroll-snap 属性,可以轻松实现全屏滚动效果:

vue实现fullpage

<template>
  <div class="scroll-container">
    <div class="section" v-for="(item, index) in sections" :key="index">
      {{ item.content }}
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      sections: [
        { content: '第一屏内容' },
        { content: '第二屏内容' },
        { content: '第三屏内容' }
      ]
    }
  }
}
</script>

<style>
.scroll-container {
  height: 100vh;
  overflow-y: scroll;
  scroll-snap-type: y mandatory;
}
.section {
  height: 100vh;
  scroll-snap-align: start;
  display: flex;
  justify-content: center;
  align-items: center;
  font-size: 2em;
  color: #fff;
}
</style>

注意事项

  1. 使用第三方库时要注意许可证问题,某些功能可能需要购买许可证
  2. 移动端触摸事件需要额外处理,确保滑动体验流畅
  3. 性能优化很重要,特别是当页面内容较多时
  4. 考虑浏览器兼容性,特别是使用 CSS scroll-snap 时

以上方法可以根据项目需求选择使用,第三方库提供更多功能但增加包体积,原生实现更轻量但需要处理更多细节。

标签: vuefullpage
分享给朋友:

相关文章

vue实现继承语法糖

vue实现继承语法糖

在Vue中实现继承可以通过多种方式模拟类继承的效果,以下是常见的几种方法: 使用mixins实现逻辑复用 通过Vue的mixins机制共享组件选项,达到类似继承的效果: const BaseCom…

vue 实现递归

vue 实现递归

Vue 实现递归组件的方法 在 Vue 中实现递归组件通常用于处理树形结构数据,例如菜单、评论列表或文件目录。以下是实现递归组件的关键步骤: 定义递归组件 递归组件需要设置 name 属性,以便在模…

vue实现全屏

vue实现全屏

Vue 实现全屏功能的方法 在 Vue 中实现全屏功能可以通过浏览器提供的 Fullscreen API 来实现。以下是几种常见的实现方式: 使用原生 Fullscreen API 通过调用 doc…

vue实现picker

vue实现picker

Vue 实现 Picker 组件的方法 在 Vue 中实现 Picker 组件可以通过多种方式完成,以下是几种常见的实现方法: 使用原生 HTML 和 CSS 实现 通过 Vue 的模板和样式绑定,…

vue实现数组

vue实现数组

Vue 实现数组操作的方法 在 Vue 中,数组的操作需要特别注意响应式的问题。Vue 提供了一些方法来确保数组的变动能够触发视图更新。 响应式数组方法 Vue 对以下数组方法进行了封装,确保它们能…

实现vue message

实现vue message

实现 Vue 消息提示组件 创建 Message 组件 在 src/components 目录下创建 Message.vue 文件,内容如下: <template> <tran…