当前位置:首页 > 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 封装库,提供丰富的全屏滚动功能,包括动画、导航和回调。

安装依赖:

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 是一个轻量级库,专注于实现平滑的滚动吸附效果,适合移动端和简单全屏滚动需求。

安装依赖:

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>

自定义滚动逻辑

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

vue怎么实现全屏滚动

<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-html 指令 通过 v-html 指令可以动态插入 HTML 内容,将需要高…

vue slot实现

vue slot实现

vue slot 的实现方法 在 Vue 中,slot 是一种内容分发机制,允许父组件向子组件传递模板内容。以下是几种常见的 slot 实现方式: 默认 slot 默认 slot 是最基础的 slo…

vue实现盖章

vue实现盖章

Vue 实现盖章功能 在Vue中实现盖章功能通常涉及前端UI交互,通过Canvas或SVG绘制印章,并允许用户拖拽或点击放置印章。以下是具体实现方法: 使用Canvas绘制印章 通过HTML5的Ca…

vue实现grid

vue实现grid

Vue 实现 Grid 布局的方法 使用 CSS Grid 布局 Vue 可以结合 CSS Grid 布局实现灵活的网格系统。CSS Grid 是现代浏览器原生支持的布局方案,无需额外依赖库。 &l…

vue实现动态

vue实现动态

Vue 实现动态路由的方法 在 Vue 中实现动态路由通常涉及以下几个关键步骤: 使用 Vue Router 的动态路由匹配 通过 Vue Router 的 :param 语法可以实现动态路由匹配。…

vue 实现树状

vue 实现树状

Vue 实现树状结构的方法 递归组件实现树状结构 递归组件是 Vue 中实现树状结构的常见方法。通过组件自身调用自身,可以轻松构建多层级树状结构。 <template> <ul…