当前位置:首页 > 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)

在模板中使用:

vue实现fullpage

<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 实现基础全屏滚动效果。

创建全屏滚动组件:

vue实现fullpage

<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 防止默认滚动行为冲突。

标签: vuefullpage
分享给朋友:

相关文章

vue插槽实现

vue插槽实现

插槽的基本概念 Vue插槽(Slot)是一种内容分发机制,允许父组件向子组件传递模板片段,子组件通过<slot>标签定义接收位置。插槽的核心作用是增强组件的灵活性和复用性。 默认插槽 子…

vue 组件实现

vue 组件实现

Vue 组件实现方法 单文件组件 (SFC) 使用 .vue 文件格式,包含模板、脚本和样式三部分: <template> <div class="example">{{…

vue键盘实现

vue键盘实现

监听键盘事件 在Vue中监听键盘事件可以通过v-on指令或@缩写实现。常见的键盘事件包括keydown、keyup和keypress。例如监听回车键事件: <template> &l…

vue实现tabs

vue实现tabs

Vue实现Tabs组件的方法 使用动态组件和v-for指令 在Vue中创建Tabs组件可以通过动态组件和v-for指令实现。定义一个包含tab标题和内容的数组,使用v-for渲染tab标题,并通过点击…

黑马vue实现

黑马vue实现

以下是关于使用 Vue.js 实现黑马相关功能或项目的关键方法和实践指南: 环境配置与项目初始化 安装最新版 Vue CLI 或使用 Vite 创建项目: npm create vue@lates…

vue实现cs程序

vue实现cs程序

Vue 实现 CS(客户端-服务器)程序 Vue 本身是一个前端框架,主要用于构建用户界面。要实现一个完整的 CS(客户端-服务器)程序,需要结合后端技术。以下是实现的基本思路和步骤: 前端部分(V…