当前位置:首页 > 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 的动态绑定实现基础全屏滚动效果。创建全屏容器:

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()
    }
  }
}

实现滚动方法:

vue实现页面全屏滚动

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>

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

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

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

相关文章

vue实现引导页面

vue实现引导页面

vue实现引导页面的方法 使用Vue实现引导页面可以通过多种方式完成,以下是几种常见的方法: 使用第三方库driver.js 安装driver.js库: npm install driver.j…

php实现页面跳转

php实现页面跳转

PHP实现页面跳转的方法 在PHP中,可以通过多种方式实现页面跳转,以下是几种常用的方法: header函数跳转 header("Location: target_page.php"); exit…

vue页面实现流程

vue页面实现流程

Vue页面实现流程 初始化Vue项目 使用Vue CLI或Vite创建项目,安装Vue及相关依赖。例如通过Vue CLI: npm install -g @vue/cli vue create my…

css制作音乐盒页面

css制作音乐盒页面

音乐盒页面设计思路 音乐盒页面通常需要结合音频播放功能与视觉设计。以下是实现一个基础音乐盒页面的关键步骤。 HTML结构搭建 基础HTML需包含音频播放器控件和播放列表: <!DOC…

vue实现点击页面切换

vue实现点击页面切换

Vue 实现页面切换 在 Vue 中实现页面切换通常可以通过以下几种方式实现,具体选择取决于项目需求和技术栈。 使用 Vue Router 实现页面切换 Vue Router 是 Vue 官方提供的…

vue实现页面分页

vue实现页面分页

Vue实现页面分页的方法 使用Element UI的分页组件 Element UI提供了现成的分页组件el-pagination,适合快速集成。以下是一个示例代码: <template>…