Vant CountDown 倒計(jì)時(shí)

2022-05-31 13:35 更新

引入

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

Vue.use(CountDown);

代碼演示

基本用法

time屬性表示倒計(jì)時(shí)總時(shí)長(zhǎng),單位為毫秒

<van-count-down :time="time" />
export default {
  data() {
    return {
      time: 30 * 60 * 60 * 1000
    };
  }
}

自定義格式

通過(guò)format屬性設(shè)置倒計(jì)時(shí)文本的內(nèi)容

<van-count-down :time="time" format="DD 天 HH 時(shí) mm 分 ss 秒" />

毫秒級(jí)渲染

倒計(jì)時(shí)默認(rèn)每秒渲染一次,設(shè)置millisecond屬性可以開(kāi)啟毫秒級(jí)渲染

<van-count-down millisecond :time="time" format="HH:mm:ss:SS" />

自定義樣式

通過(guò)插槽自定義倒計(jì)時(shí)的樣式,timeData對(duì)象格式見(jiàn)下方表格

<van-count-down :time="time">
  <template v-slot="timeData">
    <span class="item">{{ timeData.hours }}</span>
    <span class="item">{{ timeData.minutes }}</span>
    <span class="item">{{ timeData.seconds }}</span>
  </template>
</van-count-down>

<style>
.item {
  display: inline-block;
  width: 22px;
  margin-right: 5px;
  color: #fff;
  font-size: 12px;
  text-align: center;
  background-color: #1989fa;
}
</style>

手動(dòng)控制

通過(guò) ref 獲取到組件實(shí)例后,可以調(diào)用start、pause、reset方法

<van-count-down
  ref="countDown"
  millisecond
  :time="3000"
  :auto-start="false"
  format="ss:SSS"
  @finish="finish"
/>
<van-grid clickable>
  <van-grid-item text="開(kāi)始" icon="play-circle-o" @click="start" />
  <van-grid-item text="暫停" icon="pause-circle-o" @click="pause" />
  <van-grid-item text="重置" icon="replay" @click="reset" />
</van-grid>
import { Toast } from 'vant';

export default {
  methods: {
    start() {
      this.$refs.countDown.start();
    },
    pause() {
      this.$refs.countDown.pause();
    },
    reset() {
      this.$refs.countDown.reset();
    },
    finish() {
      Toast('倒計(jì)時(shí)結(jié)束');
    }
  }
}

API

Props

參數(shù)說(shuō)明類(lèi)型默認(rèn)值
time倒計(jì)時(shí)時(shí)長(zhǎng),單位毫秒number | string0
format時(shí)間格式stringHH:mm:ss
auto-start是否自動(dòng)開(kāi)始倒計(jì)時(shí)booleantrue
millisecond是否開(kāi)啟毫秒級(jí)渲染booleanfalse

format 格式

格式說(shuō)明
DD天數(shù)
HH小時(shí)
mm分鐘
ss秒數(shù)
S毫秒(1 位)
SS毫秒(2 位)
SSS毫秒(3 位)

Events

事件名說(shuō)明回調(diào)參數(shù)
finish倒計(jì)時(shí)結(jié)束時(shí)觸發(fā)-
change v2.4.4倒計(jì)時(shí)變化時(shí)觸發(fā)timeData

Slots

名稱(chēng)說(shuō)明SlotProps
default自定義內(nèi)容timeData

timeData 格式

名稱(chēng)說(shuō)明類(lèi)型
days剩余天數(shù)number
hours剩余小時(shí)number
minutes剩余分鐘number
seconds剩余秒數(shù)number
milliseconds剩余毫秒number

方法

通過(guò) ref 可以獲取到 CountDown 實(shí)例并調(diào)用實(shí)例方法,詳見(jiàn) 組件實(shí)例方法

方法名說(shuō)明參數(shù)返回值
start開(kāi)始倒計(jì)時(shí)--
pause暫停倒計(jì)時(shí)--
reset重設(shè)倒計(jì)時(shí),若auto-starttrue,重設(shè)后會(huì)自動(dòng)開(kāi)始倒計(jì)時(shí)--

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

在 iOS 系統(tǒng)上倒計(jì)時(shí)不生效?

如果你遇到了在 iOS 上倒計(jì)時(shí)不生效的問(wèn)題,請(qǐng)確認(rèn)在創(chuàng)建 Date 對(duì)象時(shí)沒(méi)有使用new Date('2020-01-01')這樣的寫(xiě)法,iOS 不支持以中劃線分隔的日期格式,正確寫(xiě)法是new Date('2020/01/01')。

對(duì)此問(wèn)題的詳細(xì)解釋?zhuān)?a rel="external nofollow" target="_blank" target="_blank">stackoverflow。


實(shí)例演示

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

掃描二維碼

下載編程獅App

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

編程獅公眾號(hào)