uni-app使用countdown插件實(shí)現(xiàn)倒計(jì)時(shí)
本文實(shí)例為大家分享了使用countdown插件實(shí)現(xiàn)倒計(jì)時(shí)的具體代碼,供大家參考,具體內(nèi)容如下
實(shí)現(xiàn)的效果如下:
這里實(shí)現(xiàn)的是一個(gè)活動(dòng)倒計(jì)時(shí),獲取當(dāng)前時(shí)間和活動(dòng)開始時(shí)間,相減得出的時(shí)間差就是我們需要的倒計(jì)時(shí)。使用插件很方便。
首先新建一個(gè)項(xiàng)目,選擇uni-app,模板選擇hello-uniapp,里面有官網(wǎng)的組件可以直接使用。創(chuàng)建之后將components整個(gè)文件夾復(fù)制到自己的項(xiàng)目中。
在需要使用倒計(jì)時(shí)的頁面引入組件
<script> import uniCountdown from '@/components/uni-countdown/uni-countdown.vue' export default { data() { return { d:'', h:'', m:'', n:'' } }, components:{ uniCountdown } } </script>
在頁面中放置定時(shí)器的位置
<view class="created" v-if="myData.stat == '未拍賣'"> <span>距開始剩</span> <span class="timer"> <uni-countdown :day="d" :hour="h" :minute="m" :second="s"></uni-countdown> </span> </view>
計(jì)算定時(shí)器中綁定的時(shí)間d,h,m,s
var date = new Date(); var now = date.getTime(); var star = this.myData.startTime var endDate = new Date(star); var end = endDate.getTime(); var leftTime = end-now; if (leftTime >= 0) { this.d = Math.floor(leftTime/1000/60/60/24); this.h = this.myData.validTime+Math.floor(leftTime/1000/60/60%24); this.m = Math.floor(leftTime/1000/60%60); this.s = Math.floor(leftTime/1000%60); console.log(this.d+'天'+this.h+'時(shí)'+this.m+'分'+this.s+'秒') }
完整代碼:
<template> <view class="created"> <span>距開始剩</span> <span class="timer"> <uni-countdown :day="d" :hour="h" :minute="m" :second="s"></uni-countdown> </span> </view> </template> <script> import uniCountdown from '@/components/uni-countdown/uni-countdown.vue' export default { data() { return { d:'', h:'', m:'', n:'', } }, onLoad(option){ this.init() }, methods: { init(){ var date = new Date(); var now = date.getTime();//獲得當(dāng)前時(shí)間與1970年1月1日時(shí)間相差的毫秒數(shù) var star = this.myData.startTime var endDate = new Date(star); var end = endDate.getTime();//結(jié)束時(shí)間與1970年1月1日時(shí)間相差的毫秒數(shù) var leftTime = end-now;//計(jì)算兩日期之間相差的毫秒數(shù) if (leftTime >= 0) { this.d = Math.floor(leftTime/1000/60/60/24); this.h = Math.floor(leftTime/1000/60/60%24); this.m = Math.floor(leftTime/1000/60%60); this.s = Math.floor(leftTime/1000%60); console.log(this.d+'天'+this.h+'時(shí)'+this.m+'分'+this.s+'秒') } } }, components:{ uniCountdown } } </script>
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
webpack 1.x升級(jí)過程中的踩坑總結(jié)大全
webpack1已結(jié)不再維護(hù)了,官方在主推webpack2,最近在升級(jí)webpack過程中遇到了不少的問題,所以下面這篇文章主要給大家總結(jié)了一些在webpack 1.x升級(jí)過程中的遇到的坑,以及詳細(xì)的解決方法,需要的朋友可以參考借鑒,下面來一起看看吧。2017-08-08js對(duì)數(shù)組中的數(shù)字從小到大排序?qū)崿F(xiàn)代碼
對(duì)數(shù)組中的數(shù)字從小到大排序,很多時(shí)候需要用的多,需要的朋友可以參考下2012-09-09原生javascript實(shí)現(xiàn)分享到朋友圈功能 支持ios和android
本文主要介紹網(wǎng)上一個(gè)牛人寫的js可以實(shí)現(xiàn)在UC瀏覽器和QQ瀏覽器中調(diào)用瀏覽器內(nèi)置的分享組件進(jìn)行分享。2016-05-05layui 地區(qū)三級(jí)聯(lián)動(dòng) form select 渲染的實(shí)例
今天小編就為大家分享一篇layui 地區(qū)三級(jí)聯(lián)動(dòng) form select 渲染的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2019-09-09CryptoJS中AES實(shí)現(xiàn)前后端通用加解密技術(shù)
這篇文章主要為大家詳細(xì)介紹了CryptoJS中AES實(shí)現(xiàn)前后端通用加解密技術(shù),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-12-12window.location.href = window.location.href 跳轉(zhuǎn)無反應(yīng) a超鏈接onclic
js下window.location.href = window.location.href 跳轉(zhuǎn)無反應(yīng) a 超鏈接 onclick 點(diǎn)擊跳轉(zhuǎn)無反應(yīng)問題的解決方法2013-08-08JS+cookie實(shí)現(xiàn)購物評(píng)價(jià)五星好評(píng)功能
這篇文章主要為大家詳細(xì)介紹了JS+cookie實(shí)現(xiàn)購物評(píng)價(jià)五星好評(píng)功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-09-09