能提上去么

This commit is contained in:
Logic 2019-01-29 01:03:06 +08:00
parent c9587cf72a
commit 8fb477ba22
5 changed files with 109 additions and 124 deletions

View File

@ -33,6 +33,8 @@ import FlipDown from 'vue-flip-down';
| ------- | ----------- | ---- | ------------------------------------------------------------------------------------- | | ------- | ----------- | ---- | ------------------------------------------------------------------------------------- |
| endDate | Date/Number | 0 | 结束的时间即倒计时会从当前时间一直到endDate停止可以是一个日期对象也可以是毫秒数 | | endDate | Date/Number | 0 | 结束的时间即倒计时会从当前时间一直到endDate停止可以是一个日期对象也可以是毫秒数 |
| type | Number | 4 | 要怎么显示倒计时4-日时分秒3-时分秒2-分秒1-秒 | | type | Number | 4 | 要怎么显示倒计时4-日时分秒3-时分秒2-分秒1-秒 |
|theme|Number|1|样式1-合并2-分离。见下图|
|timeUnit|Array|\[\]|时间单位,显示在空隙之间的文字,比如:\['天','时','分','秒'\] 或 \[':',':',':'\]|
### 事件 ### 事件
| 名称 | 返回值 | 描述 | | 名称 | 返回值 | 描述 |

2
dist/main.js vendored

File diff suppressed because one or more lines are too long

View File

@ -2,6 +2,8 @@
<div> <div>
<CountDown :endDate="endDate" <CountDown :endDate="endDate"
:type="type" :type="type"
:theme="21"
@timeUp="onTimeUp" /> @timeUp="onTimeUp" />
<hr /> <hr />
@ -15,7 +17,7 @@ export default {
data() { data() {
return { return {
type: 4, type: 4,
endDate: new Date().getTime(), endDate: new Date().getTime() + 30000,
}; };
}, },
components: { components: {

View File

@ -1,6 +1,6 @@
{ {
"name": "vue-flip-down", "name": "vue-flip-down",
"version": "1.0.1", "version": "1.0.2",
"description": "vue 翻页效果的倒计时组件", "description": "vue 翻页效果的倒计时组件",
"main": "dist/main.js", "main": "dist/main.js",
"scripts": { "scripts": {

View File

@ -1,68 +1,24 @@
<!-- 翻页效果 倒计时组件 --> <!-- 翻页效果 倒计时组件 -->
<template> <template>
<div class="vue-countdown-component"> <div :class="['vue-countdown-component', {'theme2': theme !== 1}]">
<!-- --> <template v-for="(item, index) in timeArray">
<div class="time-box" <div :class="['time-box']" :key="index">
v-if="type>=4"> {{item}}
{{day}} <div :class="['b0',{'anime': isAnimate[index]}]">
<div :class="['b0',{'anime': isDayAnime}]"> <div>{{item}}</div>
<div>{{day}}</div> </div>
<div :class="['a0',{'anime': isAnimate[index]}]"
@animationend="onAnimateEnd(index)">
<div>{{timeArrayT[index]}}</div>
</div>
<div class="a1">
<div>{{timeArrayT[index]}}</div>
</div>
</div> </div>
<div :class="['a0',{'anime': isDayAnime}]" <div class="time-unit" v-if="isTimeUnitShow(index)">
@animationend="onDayAnimateEnd"> {{setTimeUnit(index)}}
<div>{{dayDelay}}</div>
</div> </div>
<div class="a1"> </template>
<div>{{dayDelay}}</div>
</div>
</div>
<!-- -->
<div class="time-box"
v-if="type>=3">
{{hour}}
<div :class="['b0',{'anime': isHourAnime}]">
<div>{{hour}}</div>
</div>
<div :class="['a0',{'anime': isHourAnime}]"
@animationend="onHourAnimateEnd">
<div>{{hourDelay}}</div>
</div>
<div class="a1">
<div>{{hourDelay}}</div>
</div>
</div>
<!-- -->
<div class="time-box"
v-if="type>=2">
{{min}}
<div :class="['b0',{'anime': isMinAnime}]">
<div>{{min}}</div>
</div>
<div :class="['a0',{'anime': isMinAnime}]"
@animationend="onMinAnimateEnd">
<div>{{minDelay}}</div>
</div>
<div class="a1">
<div>{{minDelay}}</div>
</div>
</div>
<!-- -->
<div class="time-box">
{{second}}
<div :class="['b0',{'anime': isSecondAnime}]">
<div>{{second}}</div>
</div>
<div :class="['a0',{'anime': isSecondAnime}]"
@animationend="onSecondAnimateEnd">
<div>{{secondDelay}}</div>
</div>
<div class="a1">
<div>{{secondDelay}}</div>
</div>
</div>
</div> </div>
</template> </template>
@ -70,24 +26,16 @@
export default { export default {
data() { data() {
return { return {
day: '', // timeArray: ['00','00','00','00'],
dayDelay: '', timeArrayT: ['00','00','00','00'],
hour: '', // isAnimate:[false,false,false,false],
hourDelay: '',
min: '', //
minDelay: '',
second: '', //
secondDelay: '',
timer: null, //
isDayAnime: false, //
isHourAnime: false, //
isMinAnime: false, //
isSecondAnime: false, //
}; };
}, },
props: { props: {
endDate: { type: [Date, Number, String], default: 0 }, // endDate: { type: [Date, Number, String], default: 0 }, //
type: { type: [Number, String], default: 4 }, // 4/3/2/1 type: { type: [Number, String], default: 4 }, // 4/3/2/1
theme: {type: [Number, String], default: 1},
timeUnit: {type: Array, default: ()=>[]},
}, },
computed: { computed: {
endTime() { endTime() {
@ -96,78 +44,90 @@ export default {
} }
return Number(this.endDate) > 0 ? Number(this.endDate) : 0; return Number(this.endDate) > 0 ? Number(this.endDate) : 0;
}, },
step(){
return this.theme === 1 ? 1 : 2;
},
arr(){
const length = this.timeArray.length;
const step = this.step;
const temp = [length - 1, length - step - 1, length - step * 2 -1, length - step * 3 - 1];
temp.length = this.type > 1 ? this.type : 1;
return temp;
}
}, },
watch: { watch: {
day(newV) { timeArray(newV,oldV){
this.isDayAnime = true; const diff = [];
newV.forEach((value,index)=>{
if(value !== oldV[index]){
this.$set(this.isAnimate, index, true);
diff.push({value,index});
}
});
setTimeout(() => { setTimeout(() => {
this.dayDelay = newV; diff.forEach((item)=>{
}, 350); this.$set(this.timeArrayT, item.index, item.value);
}, });
hour(newV) {
this.isHourAnime = true;
setTimeout(() => {
this.hourDelay = newV;
}, 350);
},
min(newV) {
this.isMinAnime = true;
setTimeout(() => {
this.minDelay = newV;
}, 350);
},
second(newV) {
this.isSecondAnime = true;
setTimeout(() => {
this.secondDelay = newV;
}, 350); }, 350);
}, },
endTime(newV) { endTime(newV) {
if (newV > 0) { if (newV > 0) {
this.start(); this.start(true);
} }
}, },
}, },
mounted() { mounted() {
this.start(); this.start(true);
}, },
beforeDestroy() { beforeDestroy() {
clearTimeout(this.timer); clearTimeout(this.timer);
}, },
methods: { methods: {
// //
start() { start(isFirst) {
clearTimeout(this.timer); clearTimeout(this.timer);
this.timer = setTimeout(() => { this.timer = setTimeout(() => {
let t = this.endTime - new Date().getTime(); // let t = this.endTime - new Date().getTime(); //
t = t < 0 ? 0 : t; t = t < 0 ? 0 : t;
let day = 0; // let day = 0; //
let hour = 0; // let hour = 0; //
let min = 0; // let min = 0; //
let second = 0; // let second = 0; //
let type = Number(this.type); const type = Number(this.type);
if (type >= 4) { if (type >= 4) {
day = Math.floor(t / 86400000); // day = Math.floor(t / 86400000); //
hour = Math.floor(t / 3600000 - day * 24); // hour = Math.floor(t / 3600000 - day * 24); //
min = Math.floor(t / 60000 - day * 1440 - hour * 60); // min = Math.floor(t / 60000 - day * 1440 - hour * 60); //
second = Math.floor(t / 1000 - day * 86400 - hour * 3600 - min * 60); // second = Math.floor(t / 1000 - day * 86400 - hour * 3600 - min * 60); //
} else if (type >= 3) { } else if (type >= 3) {
hour = Math.floor(t / 3600000); // hour = Math.floor(t / 3600000); //
min = Math.floor(t / 60000 - hour * 60); // min = Math.floor(t / 60000 - hour * 60); //
second = Math.floor(t / 1000 - hour * 3600 - min * 60); // second = Math.floor(t / 1000 - hour * 3600 - min * 60); //
} else if (type >= 2) { } else if (type >= 2) {
min = Math.floor(t / 60000); // min = Math.floor(t / 60000); //
second = Math.floor(t / 1000 - min * 60); // second = Math.floor(t / 1000 - min * 60); //
} else { } else {
second = Math.floor(t / 1000); // second = Math.floor(t / 1000); //
} }
this.day = String(day).padStart(2, '0'); let arr = [];
this.hour = String(hour).padStart(2, '0'); if(Number(this.theme) === 1){ //
this.min = String(min).padStart(2, '0'); type>= 4 && arr.push(String(day).padStart(2, '0'));
this.second = String(second).padStart(2, '0'); type>= 3 && arr.push(String(hour).padStart(2, '0'));
type>= 2 && arr.push(String(min).padStart(2, '0'));
arr.push(String(second).padStart(2, '0'));
} else { //
type>= 4 && arr.push(...String(day).padStart(2, '0').split(''));
type>= 3 && arr.push(...String(hour).padStart(2, '0').split(''));
type>= 2 && arr.push(...String(min).padStart(2, '0').split(''));
arr.push(...String(second).padStart(2, '0').split(''));
}
this.timeArray = arr;
if(isFirst){
this.timeArrayT = [...this.timeArray];
this.isAnimate = new Array(this.timeArray.length).fill(false);
}
if (t > 0) { if (t > 0) {
this.start(); this.start();
} else { } else {
@ -175,24 +135,31 @@ export default {
} }
}, 1000); }, 1000);
}, },
// onAnimateEnd(index){
onDayAnimateEnd() { this.$set(this.isAnimate, index, false);
this.isDayAnime = false;
}, },
onHourAnimateEnd() { isTimeUnitShow(index){
this.isHourAnime = false; if(this.arr.includes(index)){
}, if(index === this.timeArray.length - 1 && !this.timeUnit[3]){
onMinAnimateEnd() { return false;
this.isMinAnime = false; }
}, return true;
onSecondAnimateEnd() { }
this.isSecondAnime = false; return false;
}, },
setTimeUnit(index){
switch(index){
case this.timeArray.length - 1 : return this.timeUnit[3] || ''; //
case this.timeArray.length - this.step - 1: return this.timeUnit[2] || ''; //
case this.timeArray.length - this.step * 2 -1: return this.timeUnit[1] || ''; //
default: return this.timeUnit[0] || ''; //
}
}
}, },
}; };
</script> </script>
<style lang="less" scoped> <style lang="less">
.vue-countdown-component { .vue-countdown-component {
display: flex; display: flex;
@keyframes animate-filp { @keyframes animate-filp {
@ -211,6 +178,20 @@ export default {
transform: rotateX(0); transform: rotateX(0);
} }
} }
&.theme2{
.time-box{
min-width: 21px;
& + .time-box {
margin-left: 1px;
}
}
}
.time-unit{
padding: 0 4px;
color: #222;
font-size: 14px;
line-height: 30px;
}
.time-box { .time-box {
position: relative; position: relative;
box-sizing: border-box; box-sizing: border-box;