Vant PullRefresh 下拉刷新

2022-05-31 13:33 更新

引入

import Vue from 'vue';
import { PullRefresh } from 'vant';

Vue.use(PullRefresh);

代碼演示

基礎用法

下拉刷新時會觸發(fā) refresh 事件,在事件的回調(diào)函數(shù)中可以進行同步或異步操作,操作完成后將 v-model 設置為 false,表示加載完成。

<van-pull-refresh v-model="isLoading" @refresh="onRefresh">
  <p>刷新次數(shù): {{ count }}</p>
</van-pull-refresh>
import { Toast } from 'vant';

export default {
  data() {
    return {
      count: 0,
      isLoading: false
    }
  },
  methods: {
    onRefresh() {
      setTimeout(() => {
        Toast('刷新成功');
        this.isLoading = false;
        this.count++;
      }, 1000);
    }
  }
}

成功提示

通過success-text可以設置刷新成功后的頂部提示文案

<van-pull-refresh
  v-model="isLoading"
  success-text="刷新成功"
  @refresh="onRefresh"
>
  <p>刷新次數(shù): {{ count }}</p>
</van-pull-refresh>

自定義提示

通過插槽可以自定義下拉刷新過程中的提示內(nèi)容

<van-pull-refresh v-model="isLoading" :head-height="80" @refresh="onRefresh">
  <!-- 下拉提示,通過 scale 實現(xiàn)一個縮放效果 -->
  <img
    class="doge"
    slot="pulling"
    slot-scope="props"
    src="https://img.yzcdn.cn/vant/doge.png" rel="external nofollow"  rel="external nofollow" 
    :style="{ transform: `scale(${props.distance / 80})` }"
  >
  <!-- 釋放提示 -->
  <img
    class="doge"
    slot="loosing"
    src="https://img.yzcdn.cn/vant/doge.png" rel="external nofollow"  rel="external nofollow" 
  >
  <!-- 加載提示 -->
  <img
    class="doge"
    slot="loading"
    src="https://img.yzcdn.cn/vant/doge-fire.jpg" rel="external nofollow" 
  >
  <p>刷新次數(shù): {{ count }}</p>
</van-pull-refresh>

<style>
.doge {
  width: 140px;
  height: 72px;
  margin-top: 8px;
  border-radius: 4px;
}
</style>

API

Props

參數(shù)說明類型默認值
v-model是否處于加載中狀態(tài)boolean-
pulling-text下拉過程提示文案string下拉即可刷新...
loosing-text釋放過程提示文案string釋放即可刷新...
loading-text加載過程提示文案string加載中...
success-text刷新成功提示文案string-
success-duration刷新成功提示展示時長(ms)number | string500
animation-duration動畫時長number | string300
head-height v2.4.2頂部內(nèi)容高度number | string50
disabled是否禁用下拉刷新booleanfalse

Events

事件名說明回調(diào)參數(shù)
refresh下拉刷新時觸發(fā)-

Slots

名稱說明SlotProps
default自定義內(nèi)容-
normal非下拉狀態(tài)時頂部內(nèi)容-
pulling下拉過程中頂部內(nèi)容{ distance: 當前下拉距離 }
loosing釋放過程中頂部內(nèi)容{ distance: 當前下拉距離 }
loading加載過程中頂部內(nèi)容{ distance: 當前下拉距離 }
success刷新成功提示內(nèi)容-

常見問題

在桌面端無法操作組件?

參見在桌面端使用


實例演示

以上內(nèi)容是否對您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號