当前位置:首页 > VUE

vue实现全屏上下滚动

2026-02-21 22:08:43VUE

实现全屏上下滚动效果

在Vue中实现全屏上下滚动效果,可以通过监听鼠标滚轮事件或触摸事件来控制页面切换。以下是两种常见实现方式:

使用CSS和Vue指令实现

通过CSS设置全屏容器,结合Vue指令监听滚动事件:

<template>
  <div class="fullscreen-scroll" v-wheel="handleWheel">
    <div class="section" v-for="(item, index) in sections" :key="index">
      {{ item.content }}
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      sections: [
        { content: '第一屏内容' },
        { content: '第二屏内容' },
        { content: '第三屏内容' }
      ],
      currentIndex: 0,
      isScrolling: false
    }
  },
  directives: {
    wheel: {
      inserted(el, binding) {
        el.addEventListener('wheel', binding.value)
      },
      unbind(el, binding) {
        el.removeEventListener('wheel', binding.value)
      }
    }
  },
  methods: {
    handleWheel(e) {
      if (this.isScrolling) return

      this.isScrolling = true
      if (e.deltaY > 0 && this.currentIndex < this.sections.length - 1) {
        this.currentIndex++
      } else if (e.deltaY < 0 && this.currentIndex > 0) {
        this.currentIndex--
      }

      window.scrollTo({
        top: window.innerHeight * this.currentIndex,
        behavior: 'smooth'
      })

      setTimeout(() => {
        this.isScrolling = false
      }, 800)
    }
  }
}
</script>

<style>
.fullscreen-scroll {
  height: 100vh;
  overflow: hidden;
}

.section {
  height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
  font-size: 2rem;
}
</style>

使用第三方库实现

对于更复杂的需求,可以使用专门的全屏滚动库如vue-fullpage.js

vue实现全屏上下滚动

  1. 安装依赖:

    npm install vue-fullpage.js
  2. 在Vue项目中使用:

    vue实现全屏上下滚动

    
    <template>
    <full-page :options="options" ref="fullpage">
     <div class="section">第一屏内容</div>
     <div class="section">第二屏内容</div>
     <div class="section">第三屏内容</div>
    </full-page>
    </template>
import VueFullPage from 'vue-fullpage.js'

export default { components: { 'full-page': VueFullPage }, data() { return { options: { menu: '#menu', anchors: ['page1', 'page2', 'page3'], sectionsColor: ['#41b883', '#ff5f45', '#0798ec'], navigation: true, scrollBar: false } } } }

.section { text-align: center; font-size: 3em; } ```

移动端触摸支持

对于移动设备,需要添加触摸事件支持:

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) return

    if (diff > 0 && this.currentIndex < this.sections.length - 1) {
      this.currentIndex++
    } else if (diff < 0 && this.currentIndex > 0) {
      this.currentIndex--
    }

    window.scrollTo({
      top: window.innerHeight * this.currentIndex,
      behavior: 'smooth'
    })
  }
}

在模板中添加相应的事件监听:

<div 
  class="fullscreen-scroll" 
  v-wheel="handleWheel"
  @touchstart="handleTouchStart"
  @touchend="handleTouchEnd"
>

性能优化建议

  • 使用requestAnimationFrame替代setTimeout实现更流畅的动画
  • 添加防抖机制避免快速滚动导致的多次触发
  • 对于内容较多的页面,考虑使用懒加载技术
  • 在CSS中使用will-change: transform提升动画性能

以上方法可以根据项目需求选择使用,第三方库方案通常更全面但会增加包体积,原生实现则更轻量但需要自行处理更多细节。

标签: 全屏上下
分享给朋友:

相关文章

vue页面实现全屏功能

vue页面实现全屏功能

实现全屏功能的方法 在Vue中实现全屏功能可以通过浏览器提供的Fullscreen API完成。以下是几种常见的实现方式: 使用原生Fullscreen API // 进入全屏 function e…

vue怎么实现全屏滚动

vue怎么实现全屏滚动

Vue 实现全屏滚动的方法 使用原生 CSS 和 Vue 实现 通过 Vue 的指令或组件结合 CSS 的 scroll-snap 属性,可以实现全屏滚动效果。这种方法不需要额外依赖库,适合简单场景。…

vue实现上下循环滚动

vue实现上下循环滚动

实现上下循环滚动的Vue组件 使用CSS动画和Vue的动态绑定实现无限循环滚动效果。以下是一个基础实现方案: <template> <div class="scroll…

vue实现上下切换功能

vue实现上下切换功能

实现上下切换功能的方法 在Vue中实现上下切换功能可以通过多种方式完成,以下是几种常见的实现方法。 使用v-for和数组索引控制 通过维护一个数组和当前索引,利用按钮或键盘事件切换显示内容。…

react全屏实现

react全屏实现

使用浏览器全屏API实现 React中可以通过调用浏览器的全屏API实现全屏功能。需要获取DOM元素引用,调用requestFullscreen方法。 import { useRef } from…

React实现弹框全屏

React实现弹框全屏

React 实现弹框全屏的方法 使用 CSS 样式控制全屏 通过设置弹框的 CSS 样式,使其覆盖整个视口。可以使用 position: fixed 和 width、height 设置为 100%。…