当前位置:首页 > 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实现放大效果

使用 CSS transform 实现放大效果 在 Vue 中实现放大效果可以通过 CSS 的 transform: scale() 属性。这种方法简单高效,适合大多数交互场景。 定义一个 CSS…

vue里实现增删改查

vue里实现增删改查

在Vue中实现增删改查 数据初始化 在Vue组件的data中定义初始数据和表单绑定的变量: data() { return { items: [ { id: 1, name…

vue实现上移下移插件

vue实现上移下移插件

实现上移下移功能的 Vue 插件 在 Vue 中实现列表项的上移和下移功能,可以通过自定义指令或组件封装的方式完成。以下是两种常见实现方式: 基于数组操作的通用方法 通过操作数组索引实现元素位置交换…

vue实现星星

vue实现星星

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

vue实现钟表

vue实现钟表

使用 Vue 实现动态钟表 以下是一个基于 Vue 3 的动态钟表实现方案,包含时分秒指针和数字时间显示。 模板部分 <template> <div class="clock…

vue实现主页

vue实现主页

Vue 实现主页的基本步骤 创建一个 Vue 主页通常涉及以下几个关键步骤,从项目初始化到页面布局和功能实现。 初始化 Vue 项目 使用 Vue CLI 或 Vite 初始化项目。Vue CLI…