当前位置:首页 > 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 优化滚动动画:

vue实现全屏滚动

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 CLI 或 Vite 初始化项目,安装必要依赖(如 Vue 3、Element UI 或 Vuetify 用于 UI 组件)。 npm c…

antd vue 实现

antd vue 实现

使用 Ant Design Vue 实现功能 Ant Design Vue 是基于 Vue.js 的 UI 组件库,提供了丰富的组件和设计规范。以下是一些常见功能的实现方法。 安装 Ant Desi…

vue实现闪烁

vue实现闪烁

Vue实现元素闪烁效果 使用CSS动画实现 通过Vue绑定class结合CSS动画实现闪烁效果,代码简洁且性能较好。 <template> <div :class="{ 'bl…

vue 实现过滤

vue 实现过滤

Vue 实现过滤的方法 在 Vue 中实现过滤功能可以通过多种方式完成,包括使用计算属性、自定义过滤器、第三方库等。以下是几种常见的实现方法: 使用计算属性进行过滤 计算属性是 Vue 中最常用的过…

vue实现波形

vue实现波形

实现波形效果的方法 在Vue中实现波形效果可以通过多种方式完成,常见的方法包括使用Canvas绘制、CSS动画或第三方库。以下是几种实现方案: 使用Canvas绘制波形 通过Canvas API动态…

实现vue datepicker

实现vue datepicker

安装依赖 使用 vue-datepicker 或第三方库如 v-calendar 或 vue2-datepicker。以 vue2-datepicker 为例: npm install vue2…