当前位置:首页 > 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 实现grid

vue 实现grid

在Vue中实现Grid布局可以通过多种方式完成,以下是几种常见的方法: 使用CSS Grid布局 CSS Grid是一种强大的布局系统,可以直接在Vue组件的样式中使用。以下是一个简单的示例: &…

分页实现vue

分页实现vue

分页实现(Vue) 在Vue中实现分页功能通常需要结合前端分页逻辑和后端API支持。以下是两种常见的实现方式: 前端分页实现 适用于数据量较小的情况,直接在客户端完成分页逻辑。 <templ…

vue 实现注册

vue 实现注册

Vue 实现注册功能 注册功能通常包括表单验证、数据提交和用户反馈。以下是实现注册功能的步骤: 1. 创建注册表单组件 在 Vue 项目中创建一个注册表单组件,包含用户名、邮箱、密码等输入字段。…

javascript实现vue

javascript实现vue

Vue.js 的基本实现 在 JavaScript 中实现 Vue.js 的核心功能,主要包括数据绑定、响应式系统和虚拟 DOM。以下是一个简化的实现示例。 数据响应式系统 通过 Object.de…

vue实现slidetoggle

vue实现slidetoggle

Vue 实现 SlideToggle 效果 SlideToggle 是一种常见的交互效果,元素以滑动方式展开或收起。以下是几种实现方法: 使用 CSS Transition 和 v-show 通过…

vue实现hovuer

vue实现hovuer

Vue 实现 Hover 效果 在 Vue 中实现 hover 效果可以通过多种方式完成,以下是几种常见的方法: 使用 CSS 直接控制 通过 :hover 伪类实现,无需额外的 JavaScrip…