当前位置:首页 > VUE

vue怎么实现全屏滚动

2026-01-21 19:00:32VUE

Vue 实现全屏滚动的方法

使用原生 CSS 和 Vue 实现

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

<template>
  <div class="scroll-container">
    <div class="scroll-page" v-for="(page, index) in pages" :key="index">
      {{ page.content }}
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      pages: [
        { content: "Page 1" },
        { content: "Page 2" },
        { content: "Page 3" }
      ]
    };
  }
};
</script>

<style>
.scroll-container {
  height: 100vh;
  overflow-y: scroll;
  scroll-snap-type: y mandatory;
}

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

使用第三方库 vue-fullpage.js

vue-fullpage.js 是一个基于 FullPage.js 的 Vue 封装库,提供丰富的全屏滚动功能,包括动画、导航和回调。

安装依赖:

vue怎么实现全屏滚动

npm install vue-fullpage.js

示例代码:

<template>
  <full-page :options="options" ref="fullpage">
    <div class="section">First section</div>
    <div class="section">Second section</div>
    <div class="section">Third section</div>
  </full-page>
</template>

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

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

使用 vue-scroll-snap

vue-scroll-snap 是一个轻量级库,专注于实现平滑的滚动吸附效果,适合移动端和简单全屏滚动需求。

vue怎么实现全屏滚动

安装依赖:

npm install vue-scroll-snap

示例代码:

<template>
  <vue-scroll-snap>
    <div class="page">Page 1</div>
    <div class="page">Page 2</div>
    <div class="page">Page 3</div>
  </vue-scroll-snap>
</template>

<script>
import VueScrollSnap from 'vue-scroll-snap';

export default {
  components: {
    VueScrollSnap
  }
};
</script>

<style>
.page {
  height: 100vh;
  width: 100vw;
}
</style>

自定义滚动逻辑

通过监听滚动事件和计算当前视口位置,可以手动实现全屏滚动效果。这种方法灵活性高,但需要更多代码。

<template>
  <div class="container" @wheel="handleWheel" ref="container">
    <div 
      class="page" 
      v-for="(page, index) in pages" 
      :key="index"
      :style="{ transform: `translateY(${currentIndex * -100}vh)` }"
    >
      {{ page.content }}
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      pages: [
        { content: "Page 1" },
        { content: "Page 2" },
        { content: "Page 3" }
      ],
      currentIndex: 0,
      isScrolling: false
    };
  },
  methods: {
    handleWheel(e) {
      if (this.isScrolling) return;
      this.isScrolling = true;

      if (e.deltaY > 0 && this.currentIndex < this.pages.length - 1) {
        this.currentIndex++;
      } else if (e.deltaY < 0 && this.currentIndex > 0) {
        this.currentIndex--;
      }

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

<style>
.container {
  height: 100vh;
  overflow: hidden;
}

.page {
  height: 100vh;
  transition: transform 0.5s ease;
}
</style>

注意事项

  • 移动端兼容性:部分滚动库可能需要额外处理触摸事件。
  • 性能优化:全屏滚动可能涉及大量 DOM 操作,建议使用虚拟滚动技术优化性能。
  • 导航支持:如果需要导航指示器或锚点跳转,推荐使用 vue-fullpage.js 等成熟库。

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

相关文章

vue实现条件判断

vue实现条件判断

Vue 条件判断实现方法 Vue 提供了多种方式实现条件判断,可以根据不同场景选择合适的方式。 v-if / v-else-if / v-else 这是 Vue 中最常用的条件渲染指令,根据表达式的…

vue实现一个系统

vue实现一个系统

Vue 系统实现步骤 项目初始化与配置 使用 Vue CLI 或 Vite 创建项目: npm init vue@latest my-system 选择需要的配置(Router、Pinia 等)。…

vue实现关键词轮播

vue实现关键词轮播

Vue 实现关键词轮播 基础实现方案 使用 Vue 的 v-for 和 setInterval 实现基础轮播效果: <template> <div class="keywor…

vue实现画圆弧并着色

vue实现画圆弧并着色

在 Vue 中实现画圆弧并着色 使用 Canvas API 绘制圆弧 在 Vue 组件的 mounted 钩子中,通过 Canvas API 绘制圆弧。创建一个 canvas 元素并获取其上下文:…

vue前端实现下载进度

vue前端实现下载进度

实现下载进度条的基本思路 在Vue中实现下载进度条,通常需要结合XMLHttpRequest或Fetch API来监听下载进度事件。通过计算已下载数据与总数据的比例,动态更新进度条的显示。 使用XM…

vue实现发表

vue实现发表

Vue 实现发表功能 在 Vue 中实现发表功能通常涉及表单处理、数据绑定和网络请求。以下是实现步骤和代码示例: 表单设计与数据绑定 创建一个表单用于输入发表内容,使用 v-model 进行数据双向…