利用momentJs做一個倒計時組件(實例代碼)
今天抽空給大家介紹下vue和moment做的一個倒計時,具體內(nèi)容如下所示:
展示樣式:
<template> <div class="table-right flex-a-center"> <div class="time-text"> <span class="timeTextSpan" v-for="item,index of h" >{{item}}</span> <span class="timeTextSpan1" >: </span> <span class="timeTextSpan" v-for="item,index of m" >{{item}}</span> <span class="timeTextSpan1" >: </span> <span class="timeTextSpan" v-for="item,index of s" >{{item}}</span> </div> </div> </template>
<script> import moment from 'moment' export default { props: { endTime: { }, //接收得最后時間 2021-12-17 16:29:20 }, data() { //這里存放數(shù)據(jù) return { h:'00', m:'00', s:'00', timer:null }; }, watch: { endTime: { handler(e) { if (e) { let self = this clearInterval(this.timer) this.timer = setInterval(function(){self.init()},1000) } }, deep: true, immediate: true } }, mounted() { let self = this self.init() clearInterval(this.timer) this.timer = setInterval(function(){self.init()},1000) }, //方法集合 methods: { init(){ let time =moment(this.endTime).diff(moment()) if(time <= 0){ clearInterval(this.timer) this.onOver() return } let t = time / 1000; let d = Math.floor(t / (24 * 3600)); //剩余天數(shù),如果需要可以自行補上 let h = Math.floor((t - 24 * 3600 * d) / 3600) + d*24; //不需要天數(shù),把天數(shù)轉(zhuǎn)換成小時 let _h = Math.floor((t - 24 * 3600 * d) / 3600) //保留天數(shù)后得小時 let m = Math.floor((t - 24 * 3600 * d - _h * 3600) / 60); let s = Math.floor((t - 24 * 3600 * d - _h * 3600 - m * 60)); this.h = String(h).length == 1? '0'+String(h):String(h) this.m = String(m).length == 1? '0'+String(m):String(m) this.s = String(s).length == 1? '0'+String(s):String(s) }, onOver() { this.$emit('over') //倒計時結(jié)束得回調(diào) } }, beforeDestroy(){ this.timer = null clearInterval(this.timer) } } </script> <style lang='less' scoped> @import url("@/assets/css/supplier.less"); .table-right { font-size: 12px; color: #757e8a; .timeTextSpan{ display: inline-block; width: 14px; height: 22px; text-align: center; background: #F1F0F0; border-radius: 2px; margin-right: 2px; font-size: 16px; color: #ff8a2b; font-weight: bold; } .timeTextSpan1{ display: inline-block; width: 14px; text-align: center; vertical-align: bottom; color:#202D40; font-size: 16px; font-weight: bold; } .time-text { margin-left: 10px; } } </style>
到此這篇關(guān)于利用momentJs做一個倒計時組件的文章就介紹到這了,更多相關(guān)momentJs倒計時組件內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
javascript input輸入框模糊提示功能的實現(xiàn)
這篇文章主要介紹了javascript input輸入框模糊提示功能的實現(xiàn)的相關(guān)資料,希望通過本能幫助到大家,需要的朋友可以參考下2017-09-09Javascript查詢DBpedia小應(yīng)用實例學(xué)習(xí)
本文則嘗試利用SPARQLWrapper.js來讀取DBpedia的數(shù)據(jù),并顯示出來,感興趣的你可以參考下,或許對你有所幫助2013-03-03JavaScript中常用的字符串方法函數(shù)操作方法總結(jié)
這篇文章主要介紹了JavaScript中所有的字符串函數(shù)操作方法整理匯總,包括字符串的長度、連接、查找、截取、替換、分隔、轉(zhuǎn)換等處理方法,以及網(wǎng)址中獲取文件名等等,需要的朋友可以參考下2023-12-12一文帶你搞懂JavaScript中轉(zhuǎn)義字符的使用
說起轉(zhuǎn)義字符,大家最先想到的肯定是使用反斜杠,這也是我們最常見的,很多編程語言都支持。除了反斜杠以外,在前端開發(fā)中,還有其他幾種轉(zhuǎn)義字符,也是較常見的,本文將對這些做一個總結(jié)2023-02-02autojs 螞蟻森林能量自動拾取即給指定好友澆水的實現(xiàn)方法
這篇文章主要介紹了autojs 螞蟻森林能量自動拾取即給指定好友澆水的實現(xiàn)方法,本文通過圖文并茂實例代碼相結(jié)合給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-05-05