Vant PullRefresh 下拉刷新

2022-05-31 13:33 更新

引入

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

Vue.use(PullRefresh);

代碼演示

基礎(chǔ)用法

下拉刷新時(shí)會(huì)觸發(fā) refresh 事件,在事件的回調(diào)函數(shù)中可以進(jìn)行同步或異步操作,操作完成后將 v-model 設(shè)置為 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);
    }
  }
}

成功提示

通過(guò)success-text可以設(shè)置刷新成功后的頂部提示文案

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

自定義提示

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

<van-pull-refresh v-model="isLoading" :head-height="80" @refresh="onRefresh">
  <!-- 下拉提示,通過(guò) scale 實(shí)現(xiàn)一個(gè)縮放效果 -->
  <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ù)說(shuō)明類(lèi)型默認(rèn)值
v-model是否處于加載中狀態(tài)boolean-
pulling-text下拉過(guò)程提示文案string下拉即可刷新...
loosing-text釋放過(guò)程提示文案string釋放即可刷新...
loading-text加載過(guò)程提示文案string加載中...
success-text刷新成功提示文案string-
success-duration刷新成功提示展示時(shí)長(zhǎng)(ms)number | string500
animation-duration動(dòng)畫(huà)時(shí)長(zhǎng)number | string300
head-height v2.4.2頂部?jī)?nèi)容高度number | string50
disabled是否禁用下拉刷新booleanfalse

Events

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

Slots

名稱(chēng)說(shuō)明SlotProps
default自定義內(nèi)容-
normal非下拉狀態(tài)時(shí)頂部?jī)?nèi)容-
pulling下拉過(guò)程中頂部?jī)?nèi)容{ distance: 當(dāng)前下拉距離 }
loosing釋放過(guò)程中頂部?jī)?nèi)容{ distance: 當(dāng)前下拉距離 }
loading加載過(guò)程中頂部?jī)?nèi)容{ distance: 當(dāng)前下拉距離 }
success刷新成功提示內(nèi)容-

常見(jiàn)問(wèn)題

在桌面端無(wú)法操作組件?

參見(jiàn)在桌面端使用。


實(shí)例演示

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

掃描二維碼

下載編程獅App

公眾號(hào)
微信公眾號(hào)

編程獅公眾號(hào)