当前位置:首页 > VUE

vue实现下拉刷新组件

2026-03-06 20:23:24VUE

Vue 下拉刷新组件实现

基础实现原理

利用触摸事件(touchstarttouchmovetouchend)监听用户手势,结合 CSS 动画实现下拉效果。核心逻辑包括:

  • 计算下拉距离与阈值
  • 触发刷新回调
  • 恢复初始状态

原生实现代码示例

<template>
  <div 
    class="pull-refresh"
    @touchstart="onTouchStart"
    @touchmove="onTouchMove"
    @touchend="onTouchEnd"
  >
    <div class="refresh-head" :style="{ height: `${headHeight}px` }">
      <span v-if="status === 'pulling'">下拉刷新</span>
      <span v-else-if="status === 'loosing'">释放刷新</span>
      <span v-else-if="status === 'loading'">加载中...</span>
    </div>
    <slot></slot>
  </div>
</template>

<script>
export default {
  data() {
    return {
      startY: 0,
      currentY: 0,
      headHeight: 0,
      status: '', // pulling/loosing/loading
      maxHeight: 80
    }
  },
  methods: {
    onTouchStart(e) {
      this.startY = e.touches[0].pageY
    },
    onTouchMove(e) {
      this.currentY = e.touches[0].pageY
      const distance = this.currentY - this.startY

      if (distance > 0 && window.scrollY <= 0) {
        e.preventDefault()
        this.headHeight = Math.min(distance, this.maxHeight)
        this.status = distance > this.maxHeight ? 'loosing' : 'pulling'
      }
    },
    onTouchEnd() {
      if (this.status === 'loosing') {
        this.status = 'loading'
        this.headHeight = this.maxHeight
        this.$emit('refresh')
      } else {
        this.headHeight = 0
      }
    },
    finish() {
      this.status = ''
      this.headHeight = 0
    }
  }
}
</script>

<style>
.pull-refresh {
  position: relative;
  overflow: hidden;
}
.refresh-head {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: height 0.2s;
}
</style>

使用第三方库

推荐使用现成解决方案:

  1. better-scroll
    
    import BScroll from '@better-scroll/core'
    import PullDown from '@better-scroll/pull-down'

BScroll.use(PullDown)

this.scroll = new BScroll('.wrapper', { pullDownRefresh: { threshold: 50, stop: 20 } })

this.scroll.on('pullingDown', () => { fetchData().then(() => { this.scroll.finishPullDown() }) })

vue实现下拉刷新组件


2. vant 组件库:
```javascript
<van-pull-refresh v-model="isLoading" @refresh="onRefresh">
  <div>内容区域</div>
</van-pull-refresh>

优化方向

  • 添加加载动画(旋转图标、进度条等)
  • 支持自定义提示文字和样式
  • 增加防抖处理避免频繁触发
  • 考虑移动端浏览器弹性滚动的影响

注意事项

  • 需要确保容器有明确的固定高度
  • 在 iOS 上需要额外处理弹性滚动问题
  • 列表数据更新后需要手动调用 finish 方法

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

相关文章

vue 实现blog

vue 实现blog

Vue 实现博客的基本步骤 使用 Vue 实现博客可以分为前端和后端两部分,前端使用 Vue.js 框架,后端可以选择 Node.js、Python 或其他服务端语言。以下是一个基于 Vue 的博客实…

vue实现muli

vue实现muli

Vue 实现多语言(i18n)支持 在 Vue 项目中实现多语言(i18n)功能,通常使用 vue-i18n 插件。以下是具体实现步骤: 安装 vue-i18n npm install vue-i…

vue 实现拖拽

vue 实现拖拽

Vue 实现拖拽功能 使用 HTML5 原生拖拽 API HTML5 提供了原生的拖拽 API,可以通过 draggable 属性和相关事件实现拖拽功能。 <template> &l…

vue权限实现

vue权限实现

Vue 权限实现方案 基于路由的权限控制 通过路由守卫实现权限验证,在路由配置中添加meta字段标记权限要求。在router.beforeEach中检查用户权限与路由要求的匹配情况。 // rout…

vue实现骰子

vue实现骰子

Vue 实现骰子功能 创建 Vue 组件 在 Vue 项目中创建一个骰子组件,例如 Dice.vue。该组件包含骰子的外观和逻辑。 <template> <div class=…

vue翻页实现

vue翻页实现

Vue翻页实现方法 基础分页组件实现 使用v-for和计算属性实现基础分页逻辑: <template> <div> <ul> <li…