-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
102 lines (84 loc) · 2.8 KB
/
Copy pathapp.js
File metadata and controls
102 lines (84 loc) · 2.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
const timer = [
{
className: "days",
label: "Days",
},
{
className: "hours",
label: "Hours",
},
{
className: "minutes",
label: "Minutes",
},
{
className: "seconds",
label: "Seconds",
},
];
const countdownContainer = document.querySelector(".countdown");
const countToDate = new Date().setHours(new Date().getHours() + 240);
let previous;
function showTimer() {
timer.forEach((element) => {
const div = document.createElement("div");
div.classList.add(element.className);
div.innerHTML = `
<div class="flip-card">
<div class="top">00</div>
<div class="bottom">00</div>
</div>
<p class="title">${element.label}</p>
`;
countdownContainer.append(div);
});
}
showTimer();
setInterval(() => {
const currentDate = new Date();
const timeBetweenDates = Math.floor((countToDate - currentDate) / 1000);
if (timeBetweenDates !== previous) {
flipAllCards(timeBetweenDates);
}
previous = timeBetweenDates;
}, 250);
function flipAllCards(time) {
const days = Math.floor(time / (24 * 3600));
const hours = Math.floor((time / 3600) % 24);
const minutes = Math.floor((time / 60) % 60);
const seconds = Math.floor(time % 60);
const daysCard = document.querySelector(".days > .flip-card");
const hoursCard = document.querySelector(".hours > .flip-card");
const minutesCard = document.querySelector(".minutes > .flip-card");
const secondsCard = document.querySelector(".seconds > .flip-card");
flipCard(daysCard, days);
flipCard(hoursCard, hours);
flipCard(minutesCard, minutes);
flipCard(secondsCard, seconds);
}
function flipCard(flipCard, time) {
time = String(time).padStart(2, "0");
const currentValue = flipCard.querySelector(".top").innerText;
if (time == currentValue) return;
const topFlip = document.createElement("div");
topFlip.classList.add("top-flip");
topFlip.innerText = currentValue;
const bottomFlip = document.createElement("div");
bottomFlip.classList.add("bottom-flip");
bottomFlip.innerText = time;
const topHalf = flipCard.querySelector(".top");
const bottomHalf = flipCard.querySelector(".bottom");
topFlip.addEventListener("animationstart", () => {
topHalf.innerText = time;
});
topFlip.addEventListener("animationend", () => {
topFlip.remove();
});
bottomFlip.addEventListener("animationend", () => {
bottomHalf.innerText = time;
bottomFlip.remove();
});
flipCard.appendChild(topFlip);
flipCard.appendChild(bottomFlip);
}
console.log(countToDate)