当前位置:首页 > VUE

vue滑动页面怎么实现

2026-02-23 19:32:52VUE

Vue 滑动页面实现方法

使用原生 CSS 实现滚动

通过 CSS 的 overflow 属性控制容器滚动行为。适用于简单场景,无需额外依赖。

<template>
  <div class="scroll-container">
    <!-- 内容区域 -->
  </div>
</template>

<style>
.scroll-container {
  height: 100vh;
  overflow-y: auto;
}
</style>

使用 better-scroll 库

专门处理移动端滚动的轻量级库,支持惯性滚动、边界回弹等效果。

安装依赖:

npm install @better-scroll/core

示例代码:

<template>
  <div ref="wrapper" class="scroll-wrapper">
    <div class="scroll-content">
      <!-- 滚动内容 -->
    </div>
  </div>
</template>

<script>
import BScroll from '@better-scroll/core'

export default {
  mounted() {
    this.bs = new BScroll(this.$refs.wrapper, {
      scrollY: true,
      click: true
    })
  },
  beforeDestroy() {
    this.bs.destroy()
  }
}
</script>

<style>
.scroll-wrapper {
  height: 100vh;
  overflow: hidden;
}
</style>

使用 vue-virtual-scroller 实现虚拟滚动

适用于长列表场景,只渲染可视区域元素以提升性能。

安装依赖:

npm install vue-virtual-scroller

示例代码:

<template>
  <RecycleScroller
    class="scroller"
    :items="items"
    :item-size="50"
    key-field="id"
  >
    <template v-slot="{ item }">
      <!-- 单条内容模板 -->
    </template>
  </RecycleScroller>
</template>

<script>
import { RecycleScroller } from 'vue-virtual-scroller'

export default {
  components: { RecycleScroller },
  data() {
    return {
      items: [] // 数据源
    }
  }
}
</script>

<style>
.scroller {
  height: 100vh;
}
</style>

实现横向滑动

通过 CSS 的 white-spaceoverflow-x 属性组合实现。

<template>
  <div class="horizontal-scroll">
    <div class="scroll-content">
      <!-- 横向排列的内容 -->
    </div>
  </div>
</template>

<style>
.horizontal-scroll {
  width: 100%;
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
}
.scroll-content {
  display: inline-block;
  white-space: nowrap;
}
</style>

监听滚动事件

通过 @scroll 事件实现滚动监听逻辑。

<template>
  <div class="scrollable" @scroll="handleScroll">
    <!-- 内容 -->
  </div>
</template>

<script>
export default {
  methods: {
    handleScroll(e) {
      const { scrollTop, scrollHeight, clientHeight } = e.target
      if (scrollHeight - scrollTop === clientHeight) {
        // 到达底部逻辑
      }
    }
  }
}
</script>

注意事项

  • 移动端需添加 -webkit-overflow-scrolling: touch 启用惯性滚动
  • 虚拟滚动适合大数据量场景,但需要精确设置 item-size
  • better-scroll 初始化需确保 DOM 已渲染完成
  • 横向滑动需设置 white-space: nowrap 防止内容换行

vue滑动页面怎么实现

标签: 页面vue
分享给朋友:

相关文章

vue实现防抖节流

vue实现防抖节流

Vue 中实现防抖(Debounce) 防抖的核心是延迟执行函数,在连续触发时取消前一次的调用,只执行最后一次。适用于输入框搜索、窗口大小调整等场景。 方法1:使用 Lodash 的 _.debo…

vue树形实现

vue树形实现

Vue 树形组件实现方法 使用递归组件实现树形结构 递归组件是 Vue 中实现树形结构的常见方法。通过组件调用自身的方式,可以轻松构建多层嵌套的树形结构。 <template> &l…

vue alert实现

vue alert实现

使用 Vue 实现 Alert 组件 在 Vue 中实现 Alert 组件可以通过自定义组件或结合第三方库完成。以下是几种常见方法: 自定义 Alert 组件 创建一个可复用的 Alert 组件,…

vue 实现工具

vue 实现工具

Vue 实现工具的方法 使用 Vue 实现工具功能可以通过多种方式完成,具体取决于工具的类型和需求。以下是几种常见的方法: 组件化实现 创建一个独立的 Vue 组件来封装工具功能。例如,实现一…

实现vue框架

实现vue框架

Vue框架的基本实现 Vue是一个渐进式JavaScript框架,用于构建用户界面。以下是实现Vue框架的基本步骤: 安装Vue 通过CDN引入Vue是最简单的方式: <script src…

vue实现增加

vue实现增加

实现 Vue 中的新增功能 在 Vue 中实现新增功能通常涉及数据绑定、表单处理和状态管理。以下是几种常见的实现方式: 数据绑定与列表渲染 通过 v-model 绑定表单输入,使用数组的 pu…