tui-countdown.vue
8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
<template>
<view class="tui-countdown-box">
<view class="tui-countdown-item"
:style="{ background: backgroundColor, borderColor: borderColor, width: getWidth(d, width) + 'rpx', height: height + 'rpx' }"
v-if="days">
<view class="tui-countdown-time" :class="[scale ? 'tui-countdown-scale' : '']"
:style="{ fontSize: size + 'rpx', color: color, lineHeight: size + 'rpx' }">
{{ d }}
</view>
</view>
<view class="tui-countdown-colon" :class="{ 'tui-colon-pad': borderColor == 'transparent' }"
:style="{ lineHeight: colonSize + 'rpx', fontSize: colonSize + 'rpx', color: colonColor }" v-if="days">
{{ isColon ? ':' : '天' }}
</view>
<view class="tui-countdown-item"
:style="{ background: backgroundColor, borderColor: borderColor, width: getWidth(h, width) + 'rpx', height: height + 'rpx' }"
v-if="hours">
<view class="tui-countdown-time" :class="[scale ? 'tui-countdown-scale' : '']"
:style="{ fontSize: size + 'rpx', color: color, lineHeight: size + 'rpx' }">
{{ h }}
</view>
</view>
<view class="tui-countdown-colon" :class="{ 'tui-colon-pad': borderColor == 'transparent' }"
:style="{ lineHeight: colonSize + 'rpx', fontSize: colonSize + 'rpx', color: colonColor }" v-if="hours">
{{ isColon ? ':' : '时' }}
</view>
<view class="tui-countdown-item"
:style="{ background: backgroundColor, borderColor: borderColor, width: getWidth(i, width) + 'rpx', height: height + 'rpx' }"
v-if="minutes">
<view class="tui-countdown-time" :class="[scale ? 'tui-countdown-scale' : '']"
:style="{ fontSize: size + 'rpx', color: color, lineHeight: size + 'rpx' }">
{{ i }}
</view>
</view>
<view class="tui-countdown-colon" :class="{ 'tui-colon-pad': borderColor == 'transparent' }"
:style="{ lineHeight: colonSize + 'rpx', fontSize: colonSize + 'rpx', color: colonColor }" v-if="minutes">
{{ isColon ? ':' : '分' }}
</view>
<view class="tui-countdown-item"
:style="{ background: backgroundColor, borderColor: borderColor, width: getWidth(s, width) + 'rpx', height: height + 'rpx' }"
v-if="seconds">
<view class="tui-countdown-time" :class="[scale ? 'tui-countdown-scale' : '']"
:style="{ fontSize: size + 'rpx', color: color, lineHeight: size + 'rpx' }">
{{ s }}
</view>
</view>
<view class="tui-countdown-colon" :class="{ 'tui-colon-pad': borderColor == 'transparent' }"
:style="{ lineHeight: colonSize + 'rpx', fontSize: colonSize + 'rpx', color: colonColor }"
v-if="seconds && !isColon">
{{ unitEn ? 's' : '秒' }}
</view>
<view class="tui-countdown-colon"
:style="{ lineHeight: colonSize + 'rpx', fontSize: colonSize + 'rpx', color: colonColor }"
v-if="seconds && isMs && isColon">.</view>
<view class="tui-countdown__ms" :style="{
background: backgroundColor,
borderColor: borderColor,
fontSize: msSize + 'rpx',
color: msColor,
height: height + 'rpx',
width: msWidth > 0 ? msWidth + 'rpx' : 'auto'
}" v-if="seconds && isMs">
<view :class="{ 'tui-ms__list': ani }">
<view class="tui-ms__item" :style="{ height: height + 'rpx' }" v-for="(item, index) in ms" :key="index">
<view :class="[scale ? 'tui-countdown-scale' : '']">{{item}}</view>
</view>
</view>
</view>
</view>
</template>
<script>
export default {
name: 'tuiCountdown',
emits: ['end', 'time'],
props: {
//数字框宽度
width: {
type: Number,
default: 32
},
//数字框高度
height: {
type: Number,
default: 32
},
//数字框border颜色
borderColor: {
type: String,
default: '#333'
},
//数字框背景颜色
backgroundColor: {
type: String,
default: '#fff'
},
//数字框字体大小
size: {
type: Number,
default: 24
},
//数字框字体颜色
color: {
type: String,
default: '#333'
},
//是否缩放 0.9
scale: {
type: Boolean,
default: false
},
//冒号大小
colonSize: {
type: Number,
default: 28
},
//冒号颜色
colonColor: {
type: String,
default: '#333'
},
//剩余时间 (单位:秒)
time: {
type: [Number, String],
default: 0
},
//是否包含天
days: {
type: Boolean,
default: false
},
//是否包含小时
hours: {
type: Boolean,
default: true
},
//是否包含分钟
minutes: {
type: Boolean,
default: true
},
//是否包含秒
seconds: {
type: Boolean,
default: true
},
//单位用英文缩写表示 仅seconds秒数有效
unitEn: {
type: Boolean,
default: false
},
//是否展示为冒号,false为文字
isColon: {
type: Boolean,
default: true
},
//是否返回剩余时间
returnTime: {
type: Boolean,
default: false
},
//是否显示毫秒
isMs: {
type: Boolean,
default: false
},
msWidth: {
type: Number,
default: 32
},
msSize: {
type: Number,
default: 24
},
msColor: {
type: String,
default: '#333'
}
},
watch: {
time(val) {
this.clearTimer();
this.doLoop();
}
},
data() {
return {
countdown: null,
d: '0',
h: '00',
i: '00',
s: '00',
//此处若从9到1,结束需要特殊处理
ms: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
ani: false
};
},
created() {
this.clearTimer();
let seconds = Number(this.time || 0);
if (seconds > 0) {
this.doLoop();
}
},
// #ifndef VUE3
beforeDestroy() {
this.clearTimer();
},
// #endif
// #ifdef VUE3
beforeUnmount() {
this.clearTimer();
},
// #endif
methods: {
getWidth: function(num, width) {
return num > 99 ? (width / 2) * num.toString().length : width;
},
clearTimer() {
clearInterval(this.countdown);
this.countdown = null;
},
endOfTime(isStop = false) {
this.ani = false;
this.clearTimer();
if (!isStop) {
this.$emit('end', {});
}
},
doLoop(time = 0) {
let seconds = time || Number(this.time || 0);
this.ani = true;
this.countDown(seconds);
this.countdown = setInterval(() => {
seconds--;
this.countDown(seconds);
if (this.returnTime) {
this.$emit('time', {
seconds: seconds
});
}
if (seconds <= 0) {
this.endOfTime();
return;
}
}, 1000);
},
countDown(seconds) {
let [day, hour, minute, second] = [0, 0, 0, 0];
if (seconds > 0) {
day = this.days ? Math.floor(seconds / (60 * 60 * 24)) : 0;
hour = this.hours ? Math.floor(seconds / (60 * 60)) - day * 24 : 0;
minute = this.minutes ? Math.floor(seconds / 60) - hour * 60 - day * 24 * 60 : 0;
second = Math.floor(seconds) - day * 24 * 60 * 60 - hour * 60 * 60 - minute * 60;
}
hour = hour < 10 ? '0' + hour : hour;
minute = minute < 10 ? '0' + minute : minute;
second = second < 10 ? '0' + second : second;
this.d = day;
this.h = hour;
this.i = minute;
this.s = second;
},
reset(seconds = 0) {
let time = seconds || Number(this.time);
this.clearTimer();
if (time > 0) {
this.doLoop(time);
}
}
}
};
</script>
<style scoped>
.tui-countdown-box {
display: flex;
align-items: center;
}
.tui-countdown-box {
display: flex;
align-items: center;
}
.tui-countdown-item {
border: 1rpx solid;
display: flex;
align-items: center;
justify-content: center;
border-radius: 6rpx;
white-space: nowrap;
transform: translateZ(0);
}
.tui-countdown-time {
margin: 0;
padding: 0;
}
.tui-countdown-colon {
display: flex;
justify-content: center;
padding: 0 5rpx;
}
.tui-colon-pad {
padding: 0 !important;
}
.tui-countdown-scale {
transform: scale(0.9);
transform-origin: center center;
}
.tui-countdown__ms {
border: 1rpx solid;
overflow: hidden;
border-radius: 6rpx;
}
/*ms使用css3代替js频繁更新操作,性能优化*/
.tui-ms__list {
animation: loop 1s steps(10) infinite;
}
@keyframes loop {
from {
transform: translateY(0);
}
to {
transform: translateY(-100%);
}
}
.tui-ms__item {
display: flex;
align-items: center;
justify-content: center;
}
</style>