当前位置:首页 > VUE

vue实现全屏滚动

2026-01-08 07:54:48VUE

实现全屏滚动的 Vue 方法

使用第三方库 vue-fullpage.js

安装 vue-fullpage.js:

npm install vue-fullpage.js

在 Vue 项目中引入并注册:

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

Vue.use(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: {
        navigation: true,
        scrollBar: true
      }
    }
  }
}
</script>

自定义实现全屏滚动

监听鼠标滚轮事件,结合 CSS 实现全屏滚动效果:

// 在 mounted 钩子中添加事件监听
mounted() {
  window.addEventListener('wheel', this.handleScroll, { passive: false })
},
methods: {
  handleScroll(e) {
    e.preventDefault()
    const delta = e.deltaY
    const currentSection = this.currentSectionIndex
    const totalSections = this.sections.length

    if (delta > 0 && currentSection < totalSections - 1) {
      this.scrollToSection(currentSection + 1)
    } else if (delta < 0 && currentSection > 0) {
      this.scrollToSection(currentSection - 1)
    }
  },
  scrollToSection(index) {
    this.currentSectionIndex = index
    window.scrollTo({
      top: window.innerHeight * index,
      behavior: 'smooth'
    })
  }
}

CSS 关键样式

确保每个 section 占据整个视口:

.section {
  height: 100vh;
  width: 100%;
  overflow: hidden;
}

移动端触摸支持

添加 touch 事件处理:

methods: {
  handleTouchStart(e) {
    this.startY = e.touches[0].clientY
  },
  handleTouchEnd(e) {
    const endY = e.changedTouches[0].clientY
    const diff = this.startY - endY

    if (Math.abs(diff) > 50) {
      if (diff > 0) {
        this.scrollToSection(this.currentSectionIndex + 1)
      } else {
        this.scrollToSection(this.currentSectionIndex - 1)
      }
    }
  }
}

性能优化建议

使用 CSS will-change 属性提升滚动性能:

.section {
  will-change: transform;
}

考虑使用 requestAnimationFrame 优化滚动动画:

function smoothScroll(targetY) {
  const startY = window.pageYOffset
  const distance = targetY - startY
  const duration = 1000
  let start = null

  function step(timestamp) {
    if (!start) start = timestamp
    const progress = timestamp - start
    const percent = Math.min(progress / duration, 1)

    window.scrollTo(0, startY + distance * percent)

    if (progress < duration) {
      window.requestAnimationFrame(step)
    }
  }

  window.requestAnimationFrame(step)
}

vue实现全屏滚动

标签: 全屏vue
分享给朋友:

相关文章

vue实现过程

vue实现过程

Vue 的实现过程 Vue 的实现过程可以分为多个核心模块,包括响应式系统、虚拟 DOM、模板编译等。以下是 Vue 的主要实现过程分析。 响应式系统 Vue 的响应式系统基于 Object.de…

vue实现modal

vue实现modal

Vue 实现 Modal 弹窗组件 使用 Vue 实现 Modal 弹窗组件可以通过多种方式,包括原生 Vue 组件、第三方库或自定义指令。以下是几种常见方法: 原生 Vue 组件实现 创建一个基…

vue实现setinterval

vue实现setinterval

在 Vue 中使用 setInterval Vue 中可以通过生命周期钩子和方法结合 setInterval 实现定时任务。以下是一个完整的实现示例: <template> <…

分页实现vue

分页实现vue

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

vue实现翻译

vue实现翻译

Vue 实现翻译功能的方法 使用 i18n 插件实现多语言切换 安装 vue-i18n 插件: npm install vue-i18n 创建语言资源文件(如 en.json 和 zh.json):…

vue实现机制

vue实现机制

Vue 实现机制的核心原理 Vue.js 的核心实现机制主要基于响应式系统、虚拟 DOM 和组件化设计。以下是关键实现细节: 响应式系统 Vue 通过 Object.defineProperty(…