-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
132 lines (100 loc) · 4.35 KB
/
main.js
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
const dateBox = Array.from(document.querySelectorAll('span.number > span'))
const launchDate = new Date('December 2, 2022')
const getDateFromMilliseconds = milli => {
const days = Math.floor(milli / (1000 * 60 * 60 * 24))
const hours = Math.floor(milli / (1000 * 60 * 60)) - (days * 24)
const minutes = Math.floor(milli / (1000 * 60)) - (days * 24 * 60) - (hours * 60)
const seconds = Math.floor(milli / 1000) - (days * 24 * 60 * 60) - (hours * 60 * 60) - (minutes * 60)
return [days, hours, minutes, seconds]
}
const arraysEqual = (a, b) => {
return JSON.stringify(a) == JSON.stringify(b)
}
const groupedArray = (array, number) => {
let newArray = [], group
for (let i = 0; i < number; i++) {
group = array.filter((value, index) => {
return Math.floor(index / number) == i
})
newArray.push(group)
}
// array.filter((value, index) => {
// newArray[Math.floor(index/ number)][index % number] = value
// })
return newArray
}
let groupedDateBox = groupedArray(dateBox, 4)
const trans1 = 'transform 300ms ease-in, filter 300ms ease-out'
const trans0 = 'transform 0s, filter 0s'
const updateDOM = (date, prevDate) => {
const isChanged = date.map((value, index) => {
return value !== prevDate[index]
})
isChanged.forEach((value, index) => {
if (value) {
groupedDateBox[index][0].innerText = date[index]
groupedDateBox[index][1].innerText = prevDate[index]
groupedDateBox[index][2].innerText = prevDate[index]
groupedDateBox[index][3].innerText = date[index]
groupedDateBox[index][0].style.transition = trans1
groupedDateBox[index][1].style.transition = trans1
groupedDateBox[index][2].style.transition = trans1
groupedDateBox[index][3].style.transition = trans1
groupedDateBox[index][2].style.transform = 'rotateX(-180deg)'
groupedDateBox[index][3].style.transform = 'rotateX(-360deg)'
groupedDateBox[index][0].style.filter = 'brightness(100%)'
groupedDateBox[index][1].style.filter = 'brightness(50%)'
groupedDateBox[index][2].style.filter = 'brightness(0%)'
groupedDateBox[index][3].style.filter = 'brightness(100%)'
setTimeout(() => {
groupedDateBox[index][1].innerText = date[index]
groupedDateBox[index][2].innerText = date[index]
groupedDateBox[index][0].style.transition = trans0
groupedDateBox[index][1].style.transition = trans0
groupedDateBox[index][2].style.transition = trans0
groupedDateBox[index][3].style.transition = trans0
groupedDateBox[index][2].style.transform = 'rotateX(0deg)'
groupedDateBox[index][3].style.transform = 'rotateX(-180deg)'
groupedDateBox[index][0].style.filter = 'brightness(50%)'
groupedDateBox[index][1].style.filter = 'brightness(100%)'
groupedDateBox[index][2].style.filter = 'brightness(100%)'
groupedDateBox[index][3].style.filter = 'brightness(0%)'
}, 500)
}
});
// dateBox.forEach((box, index) => {
// box.innerText = date[index]
// })
}
const calcDateDiff = () => {
const currentDate = new Date()
const timeLeft = launchDate.getTime() - currentDate.getTime()
const dateDiff = getDateFromMilliseconds(timeLeft).map(value => {
if (value.toString().length < 2) return '0' + value.toString()
return value.toString()
})
return dateDiff
}
let prevDateDiff = ['00', '00', '00', '00']
const init = () => {
const dateDiff = calcDateDiff()
groupedDateBox.forEach((box, boxIndex) => {
box.forEach((value) => {
value.innerText = dateDiff[boxIndex]
})
})
prevDateDiff = dateDiff
}
const updateLoop = () => {
const currentDate = new Date()
const timeLeft = launchDate.getTime() - currentDate.getTime()
const dateDiff = getDateFromMilliseconds(timeLeft).map(value => {
if (value.toString().length < 2) return '0' + value.toString()
return value.toString()
})
!arraysEqual(prevDateDiff, dateDiff) && updateDOM(dateDiff, prevDateDiff)
prevDateDiff = dateDiff
requestAnimationFrame(updateLoop)
}
init()
requestAnimationFrame(updateLoop)