Vant3 CountDown 倒计时

介绍

用于实时展示倒计时数值,支持毫秒精度。

实例演示

引入

通过以下方式来全局注册组件,更多注册方式请参考组件注册

import { createApp } from vue;
import { CountDown } from vant;

const app = createApp();
app.use(CountDown);

代码演示

基础用法

time 属性表示倒计时总时长,单位为毫秒。

<van-count-down :time="time" />
import { ref } from vue;

export default {
  setup() {
    const time = ref(30 * 60 * 60 * 1000);
    return { time };
  },
};

自定义格式

通过 format 属性设置倒计时文本的内容。

<van-count-down :time="time" format="DD 天 HH 时 mm 分 ss 秒" />

毫秒级渲染

倒计时默认每秒渲染一次,设置 millisecond 属性可以开启毫秒级渲染。

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

自定义样式

通过插槽自定义倒计时的样式,timeData 对象格式见下方表格。

<van-count-down :time="time">
  <template #default="timeData">
    <span class="block">{{ timeData.hours }}</span>
    <span class="colon">:</span>
    <span class="block">{{ timeData.minutes }}</span>
    <span class="colon">:</span>
    <span class="block">{{ timeData.seconds }}</span>
  </template>
</van-count-down>

<style>
  .colon {
    display: inline-block;
    margin: 0 4px;
    color: #ee0a24;
  }
  .block {
    display: inline-block;
    width: 22px;
    color: #fff;
    font-size: 12px;
    text-align: center;
    background-color: #ee0a24;
  }
</style>

手动控制

通过 ref 获取到组件实例后,可以调用 start、pause、reset 方法。

<van-count-down
  ref="countDown"
  millisecond
  :time="3000"
  :auto-start="false"
  format="ss:SSS"
  @finish="onFinish"
/>
<van-grid clickable>
  <van-grid-item text="开始" 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 {
  setup() {
    const countDown = ref(null);

    const start = () => {
      countDown.value.start();
    };
    const pause = () => {
      countDown.value.pause();
    };
    const reset = () => {
      countDown.value.reset();
    };
    const onFinish = () => Toast(倒计时结束);

    return {
      start,
      pause,
      reset,
      onFinish,
      countDown,
    };
  },
};

API

Props

参数说明类型默认值
time倒计时时长,单位毫秒number | string0
format时间格式stringHH:mm:ss
auto-start是否自动开始倒计时booleantrue
millisecond是否开启毫秒级渲染booleanfalse

format 格式

格式说明
DD天数
HH小时
mm分钟
ss秒数
S毫秒(1 位)
SS毫秒(2 位)
SSS毫秒(3 位)

Events

事件名说明回调参数
finish倒计时结束时触发
change倒计时变化时触发currentTime: CurrentTime

Slots

名称说明参数
default自定义内容currentTime: CurrentTime

CurrentTime 格式

名称说明类型
total剩余总时间(单位毫秒)number
days剩余天数number
hours剩余小时number
minutes剩余分钟number
seconds剩余秒数number
milliseconds剩余毫秒number

方法

通过 ref 可以获取到 CountDown 实例并调用实例方法,详见组件实例方法

方法名说明参数返回值
start开始倒计时
pause暂停倒计时
reset重设倒计时,若 auto-start 为 true,重设后会自动开始倒计时

类型定义

通过 CountDownInstance 获取 CountDown 实例的类型定义。

import { ref } from vue;
import type { CountDownInstance } from vant;

const countDownRef = ref<CountDownInstance>();

countDownRef.value?.start();

样式变量

组件提供了下列 CSS 变量,可用于自定义样式,使用方法请参考 ConfigProvider 组件

名称默认值描述
–van-count-down-text-colorvar(–van-text-color)
–van-count-down-font-sizevar(–van-font-size-md)
–van-count-down-line-heightvar(–van-line-height-md)

常见问题

在 iOS 系统上倒计时不生效?

如果你遇到了在 iOS 上倒计时不生效的问题,请确认在创建 Date 对象时没有使用new Date(2020-01-01)这样的写法,iOS 不支持以中划线分隔的日期格式,正确写法是new Date(2020/01/01)。

对此问题的详细解释:stackoverflow

作者:唐伯虎点蚊香,如若转载,请注明出处:https://www.web176.com/vant3/18704.html

(0)
打赏 支付宝 支付宝 微信 微信
唐伯虎点蚊香的头像唐伯虎点蚊香
上一篇 2023年4月28日
下一篇 2023年4月28日

相关推荐

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注