当前位置:首页 > VUE

vue实现pc全屏滚动

2026-01-22 19:56:10VUE

实现PC全屏滚动的Vue方案

全屏滚动效果通常指页面按整屏高度分块,滚动时自动吸附到最近的区块。以下是基于Vue的实现方法:

使用vue-fullpage.js库

安装vue-fullpage.js:

npm install vue-fullpage.js --save

基础配置示例:

<template>
  <full-page :options="options">
    <div class="section">第一屏内容</div>
    <div class="section">第二屏内容</div>
    <div class="section">第三屏内容</div>
  </full-page>
</template>

<script>
import VueFullPage from 'vue-fullpage.js'

export default {
  components: {
    'full-page': VueFullPage
  },
  data() {
    return {
      options: {
        licenseKey: 'YOUR_KEY',
        scrollingSpeed: 700,
        navigation: true
      }
    }
  }
}
</script>

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

原生实现方案

通过监听滚动事件实现:

<template>
  <div class="container" ref="container">
    <div 
      v-for="(item, index) in sections" 
      :key="index"
      class="section"
      :style="{ height: `${screenHeight}px` }"
    >
      {{ item.content }}
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      screenHeight: 0,
      sections: [
        { content: '第一屏' },
        { content: '第二屏' },
        { content: '第三屏' }
      ],
      currentIndex: 0,
      isScrolling: false
    }
  },
  mounted() {
    this.screenHeight = window.innerHeight
    window.addEventListener('resize', this.handleResize)
    window.addEventListener('wheel', this.handleWheel, { passive: false })
  },
  methods: {
    handleResize() {
      this.screenHeight = window.innerHeight
    },
    handleWheel(e) {
      if (this.isScrolling) return
      e.preventDefault()

      this.isScrolling = true
      const direction = e.deltaY > 0 ? 1 : -1
      this.currentIndex = Math.max(0, 
        Math.min(this.sections.length - 1, this.currentIndex + direction))

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

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

注意事项

  • 移动端需要额外处理触摸事件
  • 考虑浏览器兼容性问题,特别是scroll-behavior属性
  • 性能优化:防抖处理滚动事件
  • 路由切换时需重置滚动位置

进阶功能实现

添加导航点交互:

<template>
  <div class="dots">
    <div 
      v-for="(item, index) in sections"
      :key="index"
      :class="{ active: currentIndex === index }"
      @click="scrollTo(index)"
    />
  </div>
</template>

<script>
methods: {
  scrollTo(index) {
    this.currentIndex = index
    window.scrollTo({
      top: index * this.screenHeight,
      behavior: 'smooth'
    })
  }
}
</script>

<style>
.dots {
  position: fixed;
  right: 20px;
  top: 50%;
  transform: translateY(-50%);
}
.dots div {
  width: 10px;
  height: 10px;
  margin: 10px 0;
  border-radius: 50%;
  background: #ccc;
  cursor: pointer;
}
.dots div.active {
  background: #333;
}
</style>

以上方案可根据实际需求选择使用库或原生实现,前者开发效率高但灵活性较低,后者可控性强但需处理更多细节。

vue实现pc全屏滚动

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

相关文章

vue实现视频会议

vue实现视频会议

使用 Vue 实现视频会议 技术选型 Vue.js 作为前端框架,结合 WebRTC 技术实现实时音视频通信。常用的库包括: peerjs:简化 WebRTC 的点对点连接。 socket.io:用…

vue实现多用户登录

vue实现多用户登录

实现多用户登录的基本思路 在Vue中实现多用户登录通常需要结合后端API完成身份验证,并通过前端路由、状态管理(如Vuex或Pinia)和本地存储(如localStorage)来管理用户会话。以下是关…

vue实现星星

vue实现星星

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

vue实现付款

vue实现付款

Vue 实现付款功能 在 Vue 中实现付款功能通常需要集成第三方支付网关(如支付宝、微信支付、Stripe 等)。以下是常见的实现方法: 集成支付宝/微信支付 安装必要的依赖(如 axios 用…

vue实现跑车

vue实现跑车

Vue 实现跑车动画效果 使用 Vue 结合 CSS 动画可以实现跑车在屏幕上移动的效果。以下是一个完整的实现方案: 创建 Vue 组件结构 <template> <div c…

vue 实现blog

vue 实现blog

Vue 实现博客的基本步骤 使用 Vue 实现博客可以分为前端和后端两部分,前端使用 Vue.js 框架,后端可以选择 Node.js、Python 或其他服务端语言。以下是一个基于 Vue 的博客实…