当前位置:首页 > VUE

vue下拉刷新实现方案

2026-02-22 09:47:52VUE

Vue 下拉刷新实现方案

使用第三方库(如 vantmescroll.js

安装 vant 库:

npm install vant

引入 van-pull-refresh 组件:

vue下拉刷新实现方案

<template>
  <van-pull-refresh v-model="isLoading" @refresh="onRefresh">
    <div>下拉刷新内容</div>
  </van-pull-refresh>
</template>

<script>
import { PullRefresh } from 'vant';
export default {
  data() {
    return {
      isLoading: false
    };
  },
  methods: {
    onRefresh() {
      setTimeout(() => {
        this.isLoading = false;
      }, 1000);
    }
  }
};
</script>

自定义实现方案

监听触摸事件实现下拉刷新:

<template>
  <div 
    class="pull-refresh" 
    @touchstart="handleTouchStart" 
    @touchmove="handleTouchMove" 
    @touchend="handleTouchEnd"
  >
    <div class="refresh-indicator" :style="{ transform: `translateY(${pullDistance}px)` }">
      {{ refreshText }}
    </div>
    <div class="content">
      <!-- 页面内容 -->
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      startY: 0,
      pullDistance: 0,
      isRefreshing: false,
      threshold: 50 // 触发刷新的阈值
    };
  },
  computed: {
    refreshText() {
      if (this.isRefreshing) return '刷新中...';
      return this.pullDistance > this.threshold ? '释放刷新' : '下拉刷新';
    }
  },
  methods: {
    handleTouchStart(e) {
      this.startY = e.touches[0].clientY;
    },
    handleTouchMove(e) {
      if (this.isRefreshing) return;
      const currentY = e.touches[0].clientY;
      this.pullDistance = Math.max(0, currentY - this.startY);
    },
    handleTouchEnd() {
      if (this.pullDistance > this.threshold) {
        this.isRefreshing = true;
        this.onRefresh();
      }
      this.pullDistance = 0;
    },
    onRefresh() {
      // 模拟异步请求
      setTimeout(() => {
        this.isRefreshing = false;
      }, 1500);
    }
  }
};
</script>

<style>
.pull-refresh {
  position: relative;
  overflow: hidden;
}
.refresh-indicator {
  text-align: center;
  padding: 10px;
  transition: transform 0.3s;
}
</style>

使用原生滚动事件

通过监听 scroll 事件实现:

vue下拉刷新实现方案

<template>
  <div class="scroll-container" ref="scrollContainer" @scroll="handleScroll">
    <div class="scroll-content">
      <div class="refresh-indicator">{{ refreshText }}</div>
      <!-- 页面内容 -->
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      isRefreshing: false,
      scrollTop: 0
    };
  },
  computed: {
    refreshText() {
      return this.isRefreshing ? '刷新中...' : '下拉刷新';
    }
  },
  methods: {
    handleScroll() {
      this.scrollTop = this.$refs.scrollContainer.scrollTop;
      if (this.scrollTop < -50 && !this.isRefreshing) {
        this.isRefreshing = true;
        this.onRefresh();
      }
    },
    onRefresh() {
      setTimeout(() => {
        this.isRefreshing = false;
      }, 1000);
    }
  }
};
</script>

<style>
.scroll-container {
  height: 100vh;
  overflow-y: auto;
}
.refresh-indicator {
  text-align: center;
  padding: 10px;
}
</style>

结合 better-scroll 实现

安装 better-scroll

npm install better-scroll

封装下拉刷新组件:

<template>
  <div class="wrapper" ref="wrapper">
    <div class="content">
      <div class="refresh-indicator">{{ refreshText }}</div>
      <!-- 页面内容 -->
    </div>
  </div>
</template>

<script>
import BScroll from 'better-scroll';
export default {
  data() {
    return {
      bs: null,
      isRefreshing: false
    };
  },
  mounted() {
    this.bs = new BScroll(this.$refs.wrapper, {
      pullDownRefresh: {
        threshold: 50,
        stop: 40
      }
    });
    this.bs.on('pullingDown', this.onRefresh);
  },
  methods: {
    onRefresh() {
      this.isRefreshing = true;
      setTimeout(() => {
        this.bs.finishPullDown();
        this.isRefreshing = false;
      }, 1000);
    }
  }
};
</script>

以上方案可根据项目需求选择,第三方库(如 vant)适合快速集成,自定义方案灵活性更高。

标签: 方案vue
分享给朋友:

相关文章

vue实现矩阵

vue实现矩阵

Vue 实现矩阵的方法 在 Vue 中实现矩阵操作可以通过多种方式完成,以下是一些常见的方法: 使用二维数组表示矩阵 矩阵可以用二维数组来表示,每个元素对应矩阵中的一个值。在 Vue 的 data…

实现.vue文件

实现.vue文件

创建.vue文件的基本结构 一个标准的.vue文件包含三个部分:模板(template)、脚本(script)和样式(style)。这种结构允许开发者在一个文件中管理组件的HTML、JavaScrip…

vue实现cs

vue实现cs

Vue 实现 CS(Client-Side)渲染的常见方法 Vue.js 是一个用于构建用户界面的渐进式框架,默认采用客户端渲染(CSR)。以下是几种常见的实现方式: 使用 Vue CLI 创建项目…

vue实现订单

vue实现订单

Vue 实现订单功能 订单数据结构设计 订单数据通常包含以下字段: order: { id: String, // 订单ID userId: String,…

vue 实现打印

vue 实现打印

Vue 实现打印功能的方法 在Vue项目中实现打印功能,可以通过以下几种方式实现: 使用window.print()方法 通过调用浏览器的原生打印API实现基础打印功能,适用于简单内容打印。 //…

vue拖拽实现

vue拖拽实现

Vue 拖拽实现方法 使用 HTML5 原生拖拽 API HTML5 提供了原生拖拽 API,可以通过 draggable 属性、dragstart、dragend、dragover 和 drop 事…