组件搬运自vant 2.8.6,抽离了 cowndown 这么一个组件
在 script
中引用组件
import CountDown from '@/components/CountDown/index.vue';
export default {
components: {CountDown}
}
在 template
中使用组件
<count-down :time="time" />
time
属性表示倒计时总时长,单位为毫秒
<count-down time="{{ time }}" />
Page({
data: {
time: 30 * 60 * 60 * 1000,
},
});
通过format
属性设置倒计时文本的内容
<count-down time="{{ time }}" format="DD 天 HH 时 mm 分 ss 秒" />
倒计时默认每秒渲染一次,设置millisecond
属性可以开启毫秒级渲染
<count-down millisecond time="{{ time }}" format="HH:mm:ss:SSS" />
设置use-slot
属性后可以自定义倒计时样式,需要通过bind:change
事件获取timeData
对象并自行渲染,格式见下方表格
<count-down class="count-down" :time="time">
<template v-slot="timeData">
<span> 活动剩余 </span>
<span class="main-color">{{ timeData.days }}</span>
<span> 天 </span>
<span class="main-color">{{ timeData.hours }}</span>
<span> 时 </span>
<span class="main-color">{{ timeData.minutes }}</span>
<span> 分 </span>
<span class="main-color">{{ timeData.seconds }}</span>
<span> 秒 </span>
</template>
</count-down>
export default {
data() {
return {
time: 30 * 60 * 60 * 1000,
}
},
};
.item {
display: inline-block;
width: 22px;
margin-right: 5px;
color: #fff;
font-size: 12px;
text-align: center;
background-color: #1989fa;
border-radius: 2px;
}
通过 selectComponent
选择器获取到组件实例后,可以调用start
、pause
、reset
方法
<count-down
class="control-count-down"
ref="countDown"
millisecond
time="{{ 3000 }}"
auto-start="{{ false }}"
format="ss:SSS"
@finish="finished"
/>
<ul>
<li @click="start">开始</li>
<li @click="pause">暂停</li>
<li @click="reset">重置</li>
</ul>
export default {
data() {
return {
time: 30 * 60 * 60 * 1000,
}
},
methods: {
start() {
const countDown = this.$ref.countDown;
countDown.start();
},
pause() {
const countDown = this.$ref.countDown;
countDown.pause();
},
reset() {
const countDown = this.$ref.countDown;
countDown.reset();
},
finished() {
Toast('倒计时结束');
},
}
};
参数 | 说明 | 类型 | 默认值 | 版本 |
---|---|---|---|---|
time | 倒计时时长,单位毫秒 | number | - | - |
format | 时间格式,DD-日,HH-时,mm-分,ss-秒,SSS-毫秒 | string | HH:mm:ss |
- |
auto-start | 是否自动开始倒计时 | boolean | true |
- |
millisecond | 是否开启毫秒级渲染 | boolean | false |
- |
use-slot | 是否使用自定义样式插槽 | boolean | false |
- |
事件名 | 说明 | 回调参数 |
---|---|---|
finish | 倒计时结束时触发 | - |
change | 时间变化时触发,仅在开启use-slot 后才会触发 |
timeData |
名称 | 说明 | 类型 |
---|---|---|
days | 剩余天数 | number |
hours | 剩余小时 | number |
minutes | 剩余分钟 | number |
seconds | 剩余秒数 | number |
milliseconds | 剩余毫秒 | number |
通过 selectComponent 可以获取到 CountDown 实例并调用实例方法
方法名 | 参数 | 返回值 | 介绍 |
---|---|---|---|
start | - | - | 开始倒计时 |
pause | - | - | 暂停倒计时 |
reset | - | - | 重设倒计时,若auto-start 为true ,重设后会自动开始倒计时 |