当前位置:首页 > 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 实现滑动效果 使用 CSS 过渡和动画 通过 Vue 的 transition 组件结合 CSS 过渡或动画实现滑动效果。适用于简单的元素入场/离场滑动。 <template>…

vue 实现全选

vue 实现全选

Vue 实现全选功能 在 Vue 中实现全选功能通常需要结合复选框的状态管理,以下是几种常见的实现方式: 使用 v-model 绑定数组 通过 v-model 绑定一个数组来管理选中的项,全选时将…

vue实现视频开场

vue实现视频开场

使用 Vue 实现视频开场动画 在 Vue 中实现视频开场动画可以通过结合 HTML5 的 <video> 标签和 Vue 的过渡效果或生命周期钩子来完成。以下是具体实现方法: 安装依赖…

vue实现多个tab

vue实现多个tab

Vue 实现多个 Tab 的方法 使用动态组件切换 在 Vue 中可以通过动态组件 <component :is="currentTab"> 结合 v-for 和 v-bind 实现多个…

前端vue登录功能实现

前端vue登录功能实现

Vue 登录功能实现步骤 创建登录表单组件 在 Vue 项目中创建一个登录组件,通常命名为 Login.vue。该组件包含用户名和密码输入框以及提交按钮。 <template> &…

vue弹幕实现不重叠

vue弹幕实现不重叠

实现 Vue 弹幕不重叠的方法 CSS 定位与动画控制 通过绝对定位和动态计算弹幕轨道高度,确保弹幕在不同轨道上运行。每条弹幕占据固定高度的轨道,避免重叠。 // 弹幕轨道高度计算 const t…