Skip to content

LimeCountDown 倒计时

  • 用于实时展示倒计时数值,支持毫秒精度。兼容uniapp/uniappx

安装

在市场导入插件即可在任意页面使用

代码演示

基础用法

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

html
<l-count-down :time="time" />
js
const time = ref(30 * 60 * 60 * 1000);

自定义格式

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

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

毫秒级渲染

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

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

自定义样式

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

html
<l-count-down :time="time">
	<template #default="{ hours, minutes, seconds }">
		<text class="block">{{ hours }}</text>
		<text class="colon">:</text>
		<text class="block">{{ minutes }}</text>
		<text class="colon">:</text>
		<text class="block">{{ seconds }}</text>
	</template>
</l-count-down>

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

手动控制

通过 ref 获取到组件实例后,可以调用 startpausereset 方法。

html
<l-count-down
  ref="countDown"
  millisecond
  :time="3000"
  :auto-start="false"
  format="ss:SSS"
  @finish="onFinish"
/>
js

const time = ref(30 * 60 * 60 * 1000)
const countDown = ref<LCountDownComponentPublicInstance | null>(null);

const start = () => {
	if (countDown.value == null) return
	countDown.value!.start();
};
const pause = () => {
	if (countDown.value == null) return
	countDown.value!.pause();
};
const reset = () => {
	if (countDown.value == null) return
	countDown.value!.reset();
};

const onFinish = () => console.log('倒计时结束');

查看示例

  • 导入后直接使用这个标签查看演示效果
vue
// 代码位于 uni_modules/lime-count-down/compoents/lime-count-down
<lime-count-down />

插件标签

  • 默认 l-count-down 为 component
  • 默认 lime-count-down 为 demo

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-starttrue,重设后会自动开始倒计时--

主题定制

样式变量

组件提供了下列 CSS 变量,可用于自定义样式。

名称默认值描述
--l-count-down-text-colorrgba(0,0,0,0.88)-
--l-count-down-font-size16px-
--l-count-down-line-height1.666-

打赏

如果你觉得本插件,解决了你的问题,赠人玫瑰,手留余香。

源代码

组件源码