vue實現(xiàn)指定日期之間的倒計時
更新時間:2021年05月20日 09:04:52 作者:進(jìn)擊的_菜雞
這篇文章主要為大家詳細(xì)介紹了vue實現(xiàn)指定日期之間的倒計時,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
本文實例為大家分享了vue實現(xiàn)指定日期之間倒計時的具體代碼,供大家參考,具體內(nèi)容如下
效果圖如下
此處使用moment.js日期處理類庫 使用方法如下
npm install moment 或者 yarn add moment
html
<div class="time-down"> <div class="back">{{dayNum}}</div> <div class="font-14 date">天</div> <div class="back">{{hourNum}}</div> <div class="font-14 date">時</div> <div class="back">{{minuteNum}}</div> <div class="font-14 date">分</div> <div class="back">{{secondNum}}</div> <div class="font-14 date">秒</div> </div>
js
import moment from 'moment'; export default { name: 'TimeRangPage', props: { startTime: String, endTime: String }, data () { return { days: 0, hours: 0, minutes: 0, seconds: 0, timeSetInterval: null, showTimeDown: false, showOver: false }; }, created () { if (moment(new Date()).isBefore(this.startTime)) { this.showTimeDown = true; this.timeDown(); } if (moment(new Date()).isAfter(this.endTime)) this.showOver = true; }, methods: { timeDown () { this.timeSetInterval = setInterval(() => { if (moment(this.startTime).isBefore(moment())) { this.showTimeDown = false; clearInterval(this.timeSetInterval); location.reload(); } let dur = moment.duration(moment(this.startTime) - moment(), 'ms'); this.days = dur.get('days'); this.hours = dur.get('hours'); this.minutes = dur.get('minutes'); this.seconds = dur.get('seconds'); }, 1000); } }, computed: { dayNum () { if (this.days < 10) return '0' + this.days; return this.days; }, hourNum () { if (this.hours < 10) return '0' + this.hours; return this.hours; }, minuteNum () { if (this.minutes < 10) return '0' + this.minutes; return this.minutes; }, secondNum () { if (this.seconds < 10) return '0' + this.seconds; return this.seconds; } } };
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Vue中filter使用及根據(jù)id刪除數(shù)組元素方式
這篇文章主要介紹了Vue中filter使用及根據(jù)id刪除數(shù)組元素方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-03-03解決vue數(shù)據(jù)不實時更新的問題(數(shù)據(jù)更改了,但數(shù)據(jù)不實時更新)
這篇文章主要介紹了解決vue數(shù)據(jù)不實時更新的問題(數(shù)據(jù)更改了,但數(shù)據(jù)不實時更新),具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-10-10vue使用高德地圖點(diǎn)擊下鉆上浮效果的實現(xiàn)思路
這篇文章主要介紹了vue使用高德地圖點(diǎn)擊下鉆 上浮效果的實現(xiàn)思路,本文以浙江省為例通過實例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下2019-10-10