0%

timeDown倒计时组件

基于vue2封装的倒计时组件,应用性高,上手即用,不用看代码

这个组件接收四个props参数

pP4ytXQ.png

1
2
3
4
const type = 'full'
const lable = ''
const postLable = '后失效'
const timeGap = end_time.value - new Date().getTime() / 1000

当type为full时:样式:

pP4yBt0.png

当type为simple时 :样式:

pP4yDhV.png

具体代码也不难,大家看不看都可以,代码可以复制即用

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
<template>
<div class="count-down-wrap" :class="type">
<div v-if="showDesc" class="count-down-text">{{lableText}}</div>
<template v-if="notSimple">
<div class="count-down-time">
<div class="count-down-day">{{countDown.day}}</div>
<div class="count-down-dot"></div>
<div class="count-down-hour">{{countDown.hour}}</div>
<div class="count-down-dot">:</div>
<div class="count-down-minute">{{countDown.minute}}</div>
<div class="count-down-dot">:</div>
<div class="count-down-second">{{countDown.second}}</div>
</div>
</template>
<template v-else-if="showSimple">
<div class="count-down-time">
<div class="count-down-minute">{{countDown.minute}}</div>
<div class="count-down-dot">:</div>
<div class="count-down-second">{{countDown.second}}</div>
</div>
</template>
<div v-if="postLable" class="post-count-down-text">{{postLable}}</div>
</div>
</template>

<script lang="ts">
import { defineComponent, ref, computed, watch, onBeforeUnmount } from 'vue'
const TIMESTAMPOFDAY = 24 * 60 * 60

export default defineComponent({
name: 'DriverSupportCountDown',
props: {
type: {
type: String
},
lable: {
type: String
},
timeGap: {
type: Number,
default: 0
},
postLable: {
type: String
}
},
setup(props, ctx) {
let countDownGap = props.timeGap || 0
const countDown = ref({
day: '0',
hour: '00',
minute: '00',
second: '00'
})
let countDownHandler = 0

const lableText = computed(() => props.lable || '')
const showDesc = computed(() => props.type === 'full' && lableText)
const notSimple = computed(() => props.type !== 'simple')
const showSimple = computed(() => props.type === 'simple')
const prefixTime = (time) => {
if (time < 10) {
return `0${time}`
}
return `${time}`
}
const updateCountDown = () => {
if (countDownGap < 0) {
ctx.emit('update')
stopCountDown()
return
}
const dayNum = parseInt(countDownGap / TIMESTAMPOFDAY + '', 10)
const hourNum = parseInt((countDownGap / 3600) % 24 + '', 10)
const minuteNum = parseInt((countDownGap / 60) % 60 + '', 10)
const secondNum = parseInt(countDownGap % 60 + '', 10)
countDown.value = {
day: dayNum + '',
hour: prefixTime(hourNum), // 60*60
minute: prefixTime(minuteNum), // 60
second: prefixTime(secondNum)
}
countDownGap -= 1
}
const startCountDown = () => {
if (!countDownGap || countDownGap <= 0) return
if (countDownHandler) {
stopCountDown()
}
updateCountDown()
countDownHandler = window.setInterval(() => {
updateCountDown()
}, 1000)
}
startCountDown()

const stopCountDown = () => {
if (countDownHandler) {
clearInterval(countDownHandler)
countDownHandler = 0
}
}
watch(() => props.timeGap, () => {
countDownGap = props.timeGap
startCountDown()
})

onBeforeUnmount(() => {
stopCountDown()
})
return {
showDesc,
lableText,
notSimple,
showSimple,
countDown
}
}
})
</script>

<style lang="stylus">
.count-down-wrap
display: flex
align-items: center
justify-content: center
margin-bottom: 10px !important
color: black
font-weight: 400;
font-size: 13px
font-family: PingFangSC-Regular
line-height: 17px
letter-spacing: 0
.count-down-text, .count-down-time
color: black
font-weight: 400
font-family: PingFangSC-Regular
letter-spacing: 0
white-space: nowrap
.count-down-time
display: flex
align-items: center
justify-content: flex-start
font-weight: 400;
font-family: PingFangSC-Regular
letter-spacing: 0
.full .count-down-text, .count-down-dot
font-weight: 400
font-family: PingFangSC-Regular
line-height: 17px;
letter-spacing: 0
.count-down-day, .count-down-hour, .count-down-minute, .count-down-second
margin: 0 4px
color: black
font-weight: 400
font-family: PingFangSC-Regular
// font-weight bold
line-height: 1
letter-spacing: 0
text-align: center
border-radius: 2px
</style>
-------------本文结束感谢您的阅读-------------
技术原创:姚渐新,您的支持将鼓励我继续创作