CountDown 倒计时
用于实时展示倒计时数值,支持毫秒精度。
基础用法
自定义格式
毫秒级渲染
自定义样式
手动控制
API
Props
| 参数 | 说明 | 类型 | 默认值 |
|---|---|---|---|
| time | 倒计时时长,单位毫秒 | number | string | 0 |
| format | 时间格式 | string | HH:mm:ss |
| auto-start | 是否自动开始倒计时 | boolean | true |
| millisecond | 是否开启毫秒级渲染 | boolean | false |
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,重设后会自动开始倒计时 | - | - |
类型定义
组件导出以下类型定义:
ts
import type {
CountDownProps,
CountDownInstance,
CountDownCurrentTime
} from 'ryxon'CountDownInstance 是组件实例的类型,用法如下:
ts
import { ref } from 'vue'
import type { CountDownInstance } from 'ryxon'
const countDownRef = ref<CountDownInstance>()
countDownRef.value?.start()主题定制
样式变量
组件提供了下列 CSS 变量,可用于自定义样式,使用方法请参考 ConfigProvider 组件。
| 名称 | 默认值 | 描述 |
|---|---|---|
| --r-count-down-text-color | var(--r-text-color) | - |
| --r-count-down-font-size | var(--r-font-size-md) | - |
| --r-count-down-line-height | var(--r-line-height-md) | - |
常见问题
在 iOS 系统上倒计时不生效?
如果你遇到了在 iOS 上倒计时不生效的问题,请确认在创建 Date 对象时没有使用new Date('2020-01-01')这样的写法,iOS 不支持以中划线分隔的日期格式,正确写法是new Date('2020/01/01')。
对此问题的详细解释:stackoverflow。
