-
Notifications
You must be signed in to change notification settings - Fork 0
/
countdown.js
55 lines (51 loc) · 1.95 KB
/
countdown.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
class NewYearCountdown {
constructor() {
this.createCountdownElement();
this.updateCountdown();
setInterval(() => this.updateCountdown(), 1000);
}
createCountdownElement() {
const countdown = document.createElement('div');
countdown.className = 'countdown';
countdown.innerHTML = `
<div class="countdown-title">距离新年还有</div>
<div class="countdown-time">
<div class="time-item">
<span class="days">00</span>
<span class="label">天</span>
</div>
<div class="time-item">
<span class="hours">00</span>
<span class="label">时</span>
</div>
<div class="time-item">
<span class="minutes">00</span>
<span class="label">分</span>
</div>
<div class="time-item">
<span class="seconds">00</span>
<span class="label">秒</span>
</div>
</div>
`;
document.querySelector('.hero-section').insertBefore(
countdown,
document.querySelector('.cta-buttons')
);
this.countdown = countdown;
}
updateCountdown() {
const now = new Date();
const currentYear = now.getFullYear();
const nextYear = new Date(currentYear + 1, 0, 1);
const diff = nextYear - now;
const days = Math.floor(diff / (1000 * 60 * 60 * 24));
const hours = Math.floor((diff % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
const minutes = Math.floor((diff % (1000 * 60 * 60)) / (1000 * 60));
const seconds = Math.floor((diff % (1000 * 60)) / 1000);
this.countdown.querySelector('.days').textContent = days.toString().padStart(2, '0');
this.countdown.querySelector('.hours').textContent = hours.toString().padStart(2, '0');
this.countdown.querySelector('.minutes').textContent = minutes.toString().padStart(2, '0');
this.countdown.querySelector('.seconds').textContent = seconds.toString().padStart(2, '0');
}
}