当前位置:首页 > VUE

vue实现下拉刷新组件

2026-01-12 07:27:08VUE

Vue 下拉刷新组件实现

核心思路
通过监听 touch 事件计算滑动距离,结合 CSS 过渡动画实现下拉效果,触发刷新回调。

基础实现步骤

监听 touch 事件
在组件 mounted 阶段绑定 touchstart/touchmove/touchend 事件:

mounted() {
  this.$el.addEventListener('touchstart', this.handleTouchStart)
  this.$el.addEventListener('touchmove', this.handleTouchMove)
  this.$el.addEventListener('touchend', this.handleTouchEnd)
}

计算滑动距离
记录初始 Y 坐标,计算移动距离:

handleTouchStart(e) {
  this.startY = e.touches[0].pageY
}

handleTouchMove(e) {
  const currentY = e.touches[0].pageY
  this.distance = currentY - this.startY
  if (this.distance > 0) {
    e.preventDefault()
  }
}

状态控制
根据距离切换不同状态:

data() {
  return {
    state: 'waiting', // waiting/pulling/loading
    distance: 0,
    startY: 0
  }
}

动画效果实现

CSS 过渡样式
为下拉区域添加平滑过渡:

.pull-refresh {
  transition: transform 0.3s ease;
}

动态位移计算
根据距离动态设置 transform:

get transformStyle() {
  return `translateY(${this.distance}px)`
}

阈值判断
设置触发刷新的最小距离(通常 50-80px):

handleTouchEnd() {
  if (this.distance > 80) {
    this.state = 'loading'
    this.$emit('refresh')
  } else {
    this.reset()
  }
}

完整组件示例

模板结构

<template>
  <div class="pull-refresh" :style="transformStyle">
    <div class="refresh-head">
      <span v-if="state === 'loading'">加载中...</span>
      <span v-else>下拉刷新</span>
    </div>
    <slot></slot>
  </div>
</template>

状态重置方法

methods: {
  reset() {
    this.distance = 0
    this.state = 'waiting'
  },
  finishRefresh() {
    this.reset()
  }
}

优化方向

节流处理
避免 touchmove 事件高频触发:

handleTouchMove: _.throttle(function(e) {
  // 计算逻辑
}, 16)

自定义插槽
支持自定义加载动画和提示文案:

<template v-slot:loading>
  <custom-spinner />
</template>

弹性效果
实现越界回弹的物理效果:

if (this.distance > 100) {
  this.distance = 100 + (this.distance - 100) * 0.5
}

第三方库推荐

  1. mescroll.js
    支持 Vue 的完整下拉刷新/上拉加载解决方案

  2. vue-pull-refresh
    轻量级组件,支持自定义动画

  3. vant-ui PullRefresh
    有赞团队开发的成熟组件

    vue实现下拉刷新组件

实现时需注意移动端事件兼容性和性能优化,建议使用 passive event listeners 提升滚动性能。

标签: 组件vue
分享给朋友:

相关文章

vue如何实现冒泡

vue如何实现冒泡

Vue 实现冒泡排序 在 Vue 中实现冒泡排序,可以通过数据绑定和响应式特性动态展示排序过程。以下是一个完整的实现示例,包含代码和详细说明。 数据准备 在 Vue 的 data 中定义一个数组作为…

vue实现倒计时抢券

vue实现倒计时抢券

Vue 实现倒计时抢券功能 核心逻辑 倒计时抢券功能需要结合时间计算、状态管理和界面交互。通过 Vue 的响应式特性和生命周期钩子,可以高效实现这一需求。 实现步骤 1. 数据准备 在 Vue 组件…

vue实现一个tab栏

vue实现一个tab栏

Vue 实现 Tab 栏 基础实现(选项式 API) <template> <div class="tab-container"> <div class="t…

vue 实现单选

vue 实现单选

使用 v-model 绑定单选值 在 Vue 中,可以通过 v-model 绑定单选按钮的值。定义一个响应式数据(如 selectedOption),并将其与单选按钮的 value 属性关联。…

vue 实现循环

vue 实现循环

Vue 实现循环的方法 在 Vue 中,可以使用 v-for 指令来实现循环渲染列表数据。以下是几种常见的循环实现方式: 基本数组循环 <template> <ul>…

vue实现watch

vue实现watch

监听数据变化 在Vue中,watch用于监听响应式数据的变化并执行回调函数。基本语法如下: watch: { propertyName(newValue, oldValue) { //…