当前位置:首页 > VUE

vue实现fullpage

2026-01-12 22:15:57VUE

Vue 实现 FullPage 滚动效果

使用 vue-fullpage.js 库

安装 vue-fullpage.js 库,这是一个专为 Vue 设计的全屏滚动插件,基于 fullPage.js 实现。

npm install vue-fullpage.js

在 Vue 项目中引入并注册组件:

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

Vue.use(VueFullPage)

在模板中使用:

<template>
  <full-page :options="options">
    <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
      }
    }
  }
}
</script>

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

自定义实现全屏滚动

通过监听鼠标滚轮事件和触摸事件,结合 CSS transform 实现基础全屏滚动效果。

创建全屏滚动组件:

<template>
  <div class="fullpage-container" @wheel="handleWheel" @touchstart="handleTouchStart" @touchend="handleTouchEnd">
    <div class="section" v-for="(section, index) in sections" :key="index" :style="{ transform: `translateY(-${currentIndex * 100}%)` }">
      {{ section.content }}
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      currentIndex: 0,
      sections: [
        { content: '第一屏' },
        { content: '第二屏' },
        { content: '第三屏' }
      ],
      touchStartY: 0
    }
  },
  methods: {
    handleWheel(e) {
      if (e.deltaY > 0 && this.currentIndex < this.sections.length - 1) {
        this.currentIndex++
      } else if (e.deltaY < 0 && this.currentIndex > 0) {
        this.currentIndex--
      }
    },
    handleTouchStart(e) {
      this.touchStartY = e.touches[0].clientY
    },
    handleTouchEnd(e) {
      const touchEndY = e.changedTouches[0].clientY
      if (touchEndY < this.touchStartY && this.currentIndex < this.sections.length - 1) {
        this.currentIndex++
      } else if (touchEndY > this.touchStartY && this.currentIndex > 0) {
        this.currentIndex--
      }
    }
  }
}
</script>

<style>
.fullpage-container {
  overflow: hidden;
  height: 100vh;
}
.section {
  height: 100vh;
  transition: transform 0.5s ease;
}
</style>

使用 CSS scroll-snap

利用现代 CSS 的 scroll-snap 特性实现原生全屏滚动效果,性能较好但浏览器兼容性需要考虑。

<template>
  <div class="scroll-container">
    <div class="section">第一屏</div>
    <div class="section">第二屏</div>
    <div class="section">第三屏</div>
  </div>
</template>

<style>
.scroll-container {
  height: 100vh;
  overflow-y: scroll;
  scroll-snap-type: y mandatory;
}
.section {
  height: 100vh;
  scroll-snap-align: start;
}
</style>

结合 Vue Router

实现全屏滚动的同时保持路由状态,适合单页应用场景。

import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)

export default new Router({
  mode: 'history',
  routes: [
    { path: '/', component: Home },
    { path: '/about', component: About },
    { path: '/contact', component: Contact }
  ],
  scrollBehavior(to, from, savedPosition) {
    if (to.hash) {
      return { selector: to.hash }
    }
    return { x: 0, y: 0 }
  }
})

性能优化建议

减少每屏中的复杂 DOM 结构和大量图片,使用 CSS 动画代替 JavaScript 动画。对于内容较多的页面,考虑懒加载技术。在移动设备上,添加 touch-action: none 防止默认滚动行为冲突。

vue实现fullpage

标签: vuefullpage
分享给朋友:

相关文章

vue实现裁剪头像

vue实现裁剪头像

Vue 实现头像裁剪功能 实现头像裁剪功能通常需要结合第三方库如 cropperjs 或 vue-cropper。以下是两种常见实现方式: 使用 vue-cropper 库 安装依赖: n…

vue查看更多怎么实现

vue查看更多怎么实现

实现 Vue 的“查看更多”功能 数据截取与显示控制 通过 v-if 或 v-show 控制内容的显示与隐藏。初始状态下只显示部分内容,点击“查看更多”后展开完整内容。 <template&g…

vue实现走势图

vue实现走势图

Vue 实现走势图的方法 使用 ECharts 库 ECharts 是一个强大的数据可视化库,支持多种图表类型,包括走势图(折线图)。在 Vue 项目中可以通过 vue-echarts 封装库或直接使…

vue实现字母添加排序

vue实现字母添加排序

实现字母添加排序的方法 在Vue中实现字母添加排序功能,可以通过以下步骤完成: 创建Vue实例并定义数据 new Vue({ el: '#app', data: { letters…

vue实现选中单元格

vue实现选中单元格

Vue 实现选中单元格的方法 基础实现思路 在 Vue 中实现选中单元格功能,通常可以通过动态绑定 class 或 style 来实现。以下是一个基于表格的简单实现示例: <templ…

vue实现倒计时抢券

vue实现倒计时抢券

Vue 实现倒计时抢券功能 核心逻辑 倒计时抢券功能需要结合时间计算、状态管理和界面交互。通过 Vue 的响应式特性和生命周期钩子,可以高效实现这一需求。 实现步骤 1. 数据准备 在 Vue 组…