当前位置:首页 > VUE

vue实现页面全屏滚动

2026-01-21 05:53:05VUE

Vue 实现页面全屏滚动

使用 vue-fullpage.js 插件

安装 vue-fullpage.js 插件,这是一个专为 Vue 设计的全屏滚动解决方案。通过 npm 或 yarn 安装:

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>

配置选项对象:

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

使用原生 CSS 和 Vue 实现

通过 CSS 的 scroll-snap 属性和 Vue 的动态绑定实现基础全屏滚动效果。创建全屏容器:

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

添加 CSS 样式:

.fullpage-container {
  height: 100vh;
  overflow-y: scroll;
  scroll-snap-type: y mandatory;
}

.section {
  height: 100vh;
  scroll-snap-align: start;
}

自定义滚动逻辑

通过监听滚动事件和 Vue 的响应式特性实现自定义滚动效果。添加滚动事件监听:

export default {
  mounted() {
    window.addEventListener('wheel', this.handleScroll, { passive: false })
  },
  beforeDestroy() {
    window.removeEventListener('wheel', this.handleScroll)
  },
  methods: {
    handleScroll(e) {
      if (e.deltaY > 0) {
        this.scrollToNext()
      } else {
        this.scrollToPrev()
      }
      e.preventDefault()
    }
  }
}

实现滚动方法:

methods: {
  scrollToNext() {
    const current = this.currentIndex
    if (current < this.sections.length - 1) {
      this.currentIndex++
      this.$refs.container.scrollTo({
        top: window.innerHeight * this.currentIndex,
        behavior: 'smooth'
      })
    }
  },
  scrollToPrev() {
    const current = this.currentIndex
    if (current > 0) {
      this.currentIndex--
      this.$refs.container.scrollTo({
        top: window.innerHeight * this.currentIndex,
        behavior: 'smooth'
      })
    }
  }
}

移动端适配

针对移动设备添加触摸事件支持:

mounted() {
  this.$refs.container.addEventListener('touchstart', this.handleTouchStart)
  this.$refs.container.addEventListener('touchend', this.handleTouchEnd)
},
methods: {
  handleTouchStart(e) {
    this.startY = e.touches[0].clientY
  },
  handleTouchEnd(e) {
    const endY = e.changedTouches[0].clientY
    if (this.startY - endY > 50) {
      this.scrollToNext()
    } else if (endY - this.startY > 50) {
      this.scrollToPrev()
    }
  }
}

性能优化

使用 Vue 的 keep-alive 缓存不活跃的页面组件:

<template>
  <div class="fullpage-container">
    <keep-alive>
      <component 
        :is="currentComponent"
        :key="currentIndex"
      />
    </keep-alive>
  </div>
</template>

添加懒加载功能,延迟加载非当前可见部分的内容:

vue实现页面全屏滚动

computed: {
  currentComponent() {
    return () => import(`@/components/Section${this.currentIndex + 1}`)
  }
}

标签: 全屏页面
分享给朋友:

相关文章

css制作锁屏页面

css制作锁屏页面

使用CSS制作锁屏页面 锁屏页面通常包含一个背景、时间显示以及可能的解锁按钮或输入框。以下是实现锁屏页面的关键CSS代码和结构。 HTML结构 <!DOCTYPE html> <…

vue实现引导页面

vue实现引导页面

实现引导页面的方法 在Vue中实现引导页面通常需要结合用户交互和页面跳转逻辑,以下是几种常见的实现方式: 使用路由守卫控制访问权限 在router.js中配置全局前置守卫,检查用户是否首次访问:…

vue实现页面手写

vue实现页面手写

Vue 实现手写功能 在 Vue 中实现手写功能通常需要使用 HTML5 的 Canvas 元素,结合鼠标或触摸事件来捕捉用户的绘制动作。以下是实现步骤和代码示例: 基础实现步骤 创建一个 Vue…

react如何清除页面

react如何清除页面

清除页面内容的方法 在React中清除页面内容通常涉及重置组件状态或移除特定元素。以下是几种常见方法: 重置组件状态 通过将组件的状态重置为初始值来清除页面内容: const MyComponen…

js 实现全屏

js 实现全屏

使用 requestFullscreen 方法 通过调用元素的 requestFullscreen 方法可以实现全屏。该方法兼容现代浏览器,但不同浏览器可能需要前缀。 const element =…

vue 实现页面 表格

vue 实现页面 表格

Vue 实现页面表格的方法 Vue 提供了多种方式实现表格功能,可以根据需求选择不同的方案。以下是常见的实现方法: 使用原生 HTML 表格 通过 Vue 的模板语法直接渲染表格数据: <t…