当前位置:首页 > 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属性
  • 性能优化:防抖处理滚动事件
  • 路由切换时需重置滚动位置

进阶功能实现

添加导航点交互:

vue实现pc全屏滚动

<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
分享给朋友:

相关文章

vue实现微博发布动态

vue实现微博发布动态

使用Vue实现微博发布动态功能 创建Vue组件结构 新建一个WeiboPost.vue组件,包含文本框、图片上传和发布按钮: <template> <div class="we…

vue 方法实现

vue 方法实现

在 Vue 中,方法的实现通常通过 methods 选项完成。以下是 Vue 方法实现的核心要点和示例: 基本方法定义 在 Vue 组件中定义方法时,需将函数声明放在 methods 对象内。这些方…

vue实现tablegrid

vue实现tablegrid

Vue 实现 TableGrid 的方法 使用 Element UI 的 Table 组件 Element UI 提供了强大的 Table 组件,可以快速实现表格布局。安装 Element UI 后,…

分页实现vue

分页实现vue

分页实现(Vue) 在Vue中实现分页功能通常需要结合前端分页逻辑和后端API支持。以下是两种常见的实现方式: 前端分页实现 适用于数据量较小的情况,直接在客户端完成分页逻辑。 <templ…

vue实现抖动

vue实现抖动

Vue 实现抖动效果 在 Vue 中实现抖动效果可以通过 CSS 动画或 JavaScript 动画库来实现。以下是几种常见的方法: 使用 CSS 动画 通过定义 @keyframes 动画并在 V…

vue实现布局

vue实现布局

Vue 实现布局方法 Vue 提供了多种方式实现页面布局,可以通过组件化、CSS 框架或自定义样式完成。以下是几种常见方法: 使用 CSS Flexbox 或 Grid 通过 Vue 单文件组件结合…