uView CountDown 倒计时

简介: uView CountDown 倒计时

该组件一般使用于某个活动的截止时间上,通过数字的变化,给用户明确的时间感受,提示用户进行某一个行为操作。

#平台差异说明

App(vue) App(nvue) H5 小程序

#基本使用

  • 通过time参数设置倒计时间,单位为ms
<template>
  <u-count-down :time="30 * 60 * 60 * 1000" format="HH:mm:ss"></u-count-down>
</template>

copy

#自定义格式

  • 说明:通过绑定change回调的值,进行自定义格式
<template>
    <u-count-down
        :time="30 * 60 * 60 * 1000"
        format="DD:HH:mm:ss"
        autoStart
        millisecond
        @change="onChange"
    >
        <view class="time">
            <text class="time__item">{{ timeData.days }}&nbsp;天</text>
            <text class="time__item">{{ timeData.hours>10?timeData.hours:'0'+timeData.hours}}&nbsp;时</text>
            <text class="time__item">{{ timeData.minutes }}&nbsp;分</text>
            <text class="time__item">{{ timeData.seconds }}&nbsp;秒</text>
        </view>
    </u-count-down>
</template>
<script>
    export default {
        data() {
            return {
                timeData: {},
            }
        },
        methods: {
            onChange(e) {
                this.timeData = e
            }
        }
    }
</script>
<style lang="scss">
.time {
    @include flex;
    align-items: center;
    &__item {
         color: #fff;
         font-size: 12px;
         text-align: center;
     }
}
</style>

copy

#毫秒级渲染

  • 通过配置millisecond来开启毫秒级倒计时
<u-count-down :time="30 * 60 * 60 * 1000" format="HH:mm:ss:SSS" autoStart millisecond></u-count-down>

copy

#自定义样式

  • 说明:通过绑定change回调的值,进行自定义格式
<template>
    <u-count-down
            :time="30 * 60 * 60 * 1000"
            format="HH:mm:ss"
            autoStart
            millisecond
            @change="onChange"
    >
        <view class="time">
            <view class="time__custom">
                <text class="time__custom__item">{{ timeData.hours>10?timeData.hours:'0'+timeData.hours}}</text>
            </view>
            <text class="time__doc">:</text>
            <view class="time__custom">
                <text class="time__custom__item">{{ timeData.minutes }}</text>
            </view>
            <text class="time__doc">:</text>
            <view class="time__custom">
                <text class="time__custom__item">{{ timeData.seconds }}</text>
            </view>
        </view>
    </u-count-down>
</template>
<script>
    export default {
        data() {
            return {
                timeData: {},
            }
        },
        methods: {
            onChange(e) {
                this.timeData = e
            }
        }
    }
</script>
<style lang="scss">
.time {
    @include flex;
    align-items: center;
    &__custom {
         margin-top: 4px;
         width: 22px;
         height: 22px;
         background-color: $u-primary;
         border-radius: 4px;
         /* #ifndef APP-NVUE */
         display: flex;
         /* #endif */
         justify-content: center;
         align-items: center;
    
        &__item {
             color: #fff;
             font-size: 12px;
             text-align: center;
         }
    }
    
    &__doc {
         color: $u-primary;
         padding: 0px 4px;
     }
    
    &__item {
         color: #606266;
         font-size: 15px;
         margin-right: 4px;
     }
}
</style>

copy

#手动控制

  • 说明:通过绑定ref进行手动控制重置、开始、暂停
<template>
    <u-count-down
        ref="countDown"
        :time="3* 1000"
        format="ss:SSS"
        :autoStart="false"
        millisecond
        @change="onChange"
    >
    </u-count-down>
    <u-button text="重置" size="normal" type="info" @click="reset"></u-button>
    <u-button text="开始" size="normal" type="success" @click="start"></u-button>
    <u-button text="暂停" size="normal" type="error" @click="pause"></u-button>
</template>
<script>
    export default {
        data() {
            return {
                timeData: {},
            }
        },
        methods: {
            //开始
            start() {
                this.$refs.countDown.start();
            },
            // 暂停
            pause() {
                this.$refs.countDown.pause();
            },
            // 重置
            reset() {
                this.$refs.countDown.reset();
            },
            onChange(e) {
                this.timeData = e
            }
        }
    }
</script>
相关文章
|
6天前
简单倒计时
简单倒计时
16 0
|
6天前
|
移动开发 小程序 API
uniapp中uview组件库丰富的CountDown 倒计时使用方法
uniapp中uview组件库丰富的CountDown 倒计时使用方法
17 2
|
6天前
|
移动开发 JavaScript 小程序
uView Notify 消息提示
uView Notify 消息提示
17 0
|
6天前
uniapp实现倒计时
uniapp实现倒计时
65 0
|
6天前
|
JavaScript
JS倒计时
JS倒计时
|
6天前
|
开发者 智能硬件
倒计时器
倒计时器
75 1
|
10月前
|
JavaScript 索引
js倒计时功能
今天使用js做一个倒计时的功能。 上代码: