当前位置:首页 > 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懒加载的实现并不复杂,核心逻辑是通过动态导入(Dynamic Imports)和路由配置或组件异步加载完成。以下是具体实现方法: 路由懒加载实现 在Vue Router…

vue实现星星

vue实现星星

实现星星评分的Vue组件 使用Vue实现星星评分功能可以通过组件化方式完成,以下是一个完整的实现方案: 模板部分 <template> <div class="star-ra…

vue实现选区

vue实现选区

Vue 实现选区的基本方法 在Vue中实现选区功能通常涉及DOM操作和事件处理。以下是几种常见的方法: 使用原生JavaScript的Selection API 通过window.getSelec…

vue列表实现

vue列表实现

Vue 列表实现方法 使用 v-for 指令 v-for 是 Vue 中用于渲染列表的核心指令,基于数据源动态生成 DOM 元素。语法格式为 item in items 或 (item, index)…

vue实现高亮

vue实现高亮

Vue 实现文本高亮的方法 在 Vue 中实现文本高亮可以通过多种方式完成,以下是几种常见的方法: 方法一:使用 v-html 指令 通过 v-html 指令可以动态插入 HTML 内容,将需要高…

vue实现jqueryui

vue实现jqueryui

Vue 实现 jQuery UI 功能 在 Vue 项目中实现类似 jQuery UI 的功能,可以通过原生 Vue 组件或第三方库来实现。以下是几种常见 jQuery UI 功能的 Vue 替代方案…