vue實(shí)現(xiàn)指定日期之間的倒計(jì)時(shí)
本文實(shí)例為大家分享了vue實(shí)現(xiàn)指定日期之間倒計(jì)時(shí)的具體代碼,供大家參考,具體內(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">時(shí)</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)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Vue寫一個(gè)簡單的倒計(jì)時(shí)按鈕功能
- 簡單實(shí)現(xiàn)vue驗(yàn)證碼60秒倒計(jì)時(shí)功能
- vue2.0實(shí)現(xiàn)倒計(jì)時(shí)的插件(時(shí)間戳 刷新 跳轉(zhuǎn) 都不影響)
- 基于vue的短信驗(yàn)證碼倒計(jì)時(shí)demo
- vue實(shí)現(xiàn)驗(yàn)證碼按鈕倒計(jì)時(shí)功能
- vue中多個(gè)倒計(jì)時(shí)實(shí)現(xiàn)代碼實(shí)例
- vue實(shí)現(xiàn)商城秒殺倒計(jì)時(shí)功能
- vue實(shí)現(xiàn)倒計(jì)時(shí)獲取驗(yàn)證碼效果
- vue設(shè)計(jì)一個(gè)倒計(jì)時(shí)秒殺的組件詳解
- vue+canvas實(shí)現(xiàn)炫酷時(shí)鐘效果的倒計(jì)時(shí)插件(已發(fā)布到npm的vue2插件,開箱即用)
相關(guān)文章
vue中插件和組件的區(qū)別點(diǎn)及用法總結(jié)
在本篇文章里小編給大家分享的是一篇關(guān)于vue中插件和組件的區(qū)別點(diǎn)及用法總結(jié)內(nèi)容,有興趣的的朋友們可以學(xué)習(xí)下。2021-12-12
關(guān)于vue-router路由的傳參方式params query
這篇文章主要介紹了關(guān)于vue-router路由的傳參方式params query,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-10-10
vant(ZanUi)結(jié)合async-validator實(shí)現(xiàn)表單驗(yàn)證的方法
這篇文章主要介紹了vant(ZanUi)結(jié)合async-validator實(shí)現(xiàn)表單驗(yàn)證的方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-12-12
解決vue router使用 history 模式刷新后404問題
這篇文章主要介紹了解決vue router使用 history 模式刷新后404問題,需要的朋友可以參考下2017-07-07
關(guān)于element-ui?單選框默認(rèn)值不選中的解決
這篇文章主要介紹了關(guān)于element-ui?單選框默認(rèn)值不選中的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-09-09
解決Vue響應(yīng)式數(shù)據(jù)已獲取而視圖不更新的問題
這篇文章主要介紹了解決Vue響應(yīng)式數(shù)據(jù)已獲取而視圖不更新的問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-08-08

