Skip to content

Commit f100eba

Browse files
iqbal-webiqbal-xs
andcommitted
Add class in countdown
Add class in countdown for customized timer Co-Authored-By: Iqbal Hossain <[email protected]>
1 parent 5e0320a commit f100eba

File tree

5 files changed

+119
-53
lines changed

5 files changed

+119
-53
lines changed

src/countdown.js

Lines changed: 36 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2,38 +2,49 @@ var Countdown = (function() {
22
var targetDate, targetElement;
33

44
function getTimeRemaining(endTime) {
5-
var totalSeconds = Math.max(Math.floor((endTime - Date.now()) / 1000), 0);
6-
var days = Math.floor(totalSeconds / (60 * 60 * 24));
7-
var hours = Math.floor((totalSeconds % (60 * 60 * 24)) / (60 * 60));
8-
var minutes = Math.floor((totalSeconds % (60 * 60)) / 60);
9-
var seconds = totalSeconds % 60;
10-
return {
11-
days: days,
12-
hours: hours,
13-
minutes: minutes,
14-
seconds: seconds
15-
};
5+
var totalSeconds = Math.max(Math.floor((endTime - Date.now()) / 1000), 0),
6+
days = Math.floor(totalSeconds / (60 * 60 * 24)),
7+
hours = Math.floor((totalSeconds % (60 * 60 * 24)) / (60 * 60)),
8+
minutes = Math.floor((totalSeconds % (60 * 60)) / 60),
9+
seconds = totalSeconds % 60;
10+
return {
11+
days: days,
12+
hours: hours,
13+
minutes: minutes,
14+
seconds: seconds
15+
};
1616
}
1717

1818
function updateCountdown() {
19-
var time = getTimeRemaining(targetDate);
20-
var countdownText = time.days + 'd ' + time.hours + 'h ' + time.minutes + 'm ' + time.seconds + 's';
21-
targetElement.innerHTML = countdownText;
19+
var time = getTimeRemaining(targetDate),
20+
countdownWrap = targetElement.querySelector('.countdown-wrap');
21+
22+
if (!countdownWrap) {
23+
countdownWrap = document.createElement('div');
24+
countdownWrap.className = 'countdown-wrap';
25+
targetElement.appendChild(countdownWrap);
26+
}
27+
28+
countdownWrap.innerHTML = `
29+
<div class="days">${time.days}d</div>
30+
<div class="hours">${time.hours}h</div>
31+
<div class="minutes">${time.minutes}m</div>
32+
<div class="seconds">${time.seconds}s</div>
33+
`;
2234
}
23-
35+
2436
function init(targetDateString, targetElementId) {
25-
targetDate = new Date(targetDateString).getTime();
26-
targetElement = document.getElementById(targetElementId);
27-
if (!targetElement) {
37+
targetDate = new Date(targetDateString).getTime();
38+
targetElement = document.getElementById(targetElementId);
39+
if (!targetElement) {
2840
console.error("Target element not found.");
2941
return;
30-
}
31-
updateCountdown();
32-
setInterval(updateCountdown, 1000);
42+
}
43+
updateCountdown();
44+
setInterval(updateCountdown, 1000);
3345
}
34-
46+
3547
return {
36-
init: init
48+
init: init
3749
};
38-
})();
39-
50+
})();

src/countdown.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/countdown.css

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/* Style the countdown wrapper */
2+
.countdown-wrap {
3+
display: flex;
4+
justify-content: center;
5+
align-items: center;
6+
font-family: Arial, sans-serif;
7+
font-size: 24px;
8+
color: #333;
9+
background-color: #f1f1f1;
10+
border-radius: 5px;
11+
padding: 10px;
12+
animation-duration: 10s; /* Duration of the rotation animation */
13+
animation-timing-function: linear; /* Linear timing */
14+
animation-iteration-count: infinite; /* Infinite loop */
15+
}
16+
17+
18+
/* Style individual countdown elements (days, hours, minutes, seconds) */
19+
.countdown-wrap div {
20+
margin: 0 10px;
21+
padding: 5px;
22+
background-color: #333;
23+
color: #fff;
24+
border-radius: 5px;
25+
}
26+
27+
28+
/* Add transition for countdown elements */
29+
.countdown-wrap div {
30+
transition: background-color 0.3s, transform 0.3s;
31+
}
32+
33+
/* Hover effect for countdown elements */
34+
.countdown-wrap div:hover {
35+
background-color: #555;
36+
transform: scale(1.1);
37+
}

test/countdown.js

Lines changed: 36 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2,38 +2,49 @@ var Countdown = (function() {
22
var targetDate, targetElement;
33

44
function getTimeRemaining(endTime) {
5-
var totalSeconds = Math.max(Math.floor((endTime - Date.now()) / 1000), 0);
6-
var days = Math.floor(totalSeconds / (60 * 60 * 24));
7-
var hours = Math.floor((totalSeconds % (60 * 60 * 24)) / (60 * 60));
8-
var minutes = Math.floor((totalSeconds % (60 * 60)) / 60);
9-
var seconds = totalSeconds % 60;
10-
return {
11-
days: days,
12-
hours: hours,
13-
minutes: minutes,
14-
seconds: seconds
15-
};
5+
var totalSeconds = Math.max(Math.floor((endTime - Date.now()) / 1000), 0),
6+
days = Math.floor(totalSeconds / (60 * 60 * 24)),
7+
hours = Math.floor((totalSeconds % (60 * 60 * 24)) / (60 * 60)),
8+
minutes = Math.floor((totalSeconds % (60 * 60)) / 60),
9+
seconds = totalSeconds % 60;
10+
return {
11+
days: days,
12+
hours: hours,
13+
minutes: minutes,
14+
seconds: seconds
15+
};
1616
}
1717

1818
function updateCountdown() {
19-
var time = getTimeRemaining(targetDate);
20-
var countdownText = time.days + 'd ' + time.hours + 'h ' + time.minutes + 'm ' + time.seconds + 's';
21-
targetElement.innerHTML = countdownText;
19+
var time = getTimeRemaining(targetDate),
20+
countdownWrap = targetElement.querySelector('.countdown-wrap');
21+
22+
if (!countdownWrap) {
23+
countdownWrap = document.createElement('div');
24+
countdownWrap.className = 'countdown-wrap';
25+
targetElement.appendChild(countdownWrap);
26+
}
27+
28+
countdownWrap.innerHTML = `
29+
<div class="days">${time.days}d</div>
30+
<div class="hours">${time.hours}h</div>
31+
<div class="minutes">${time.minutes}m</div>
32+
<div class="seconds">${time.seconds}s</div>
33+
`;
2234
}
23-
35+
2436
function init(targetDateString, targetElementId) {
25-
targetDate = new Date(targetDateString).getTime();
26-
targetElement = document.getElementById(targetElementId);
27-
if (!targetElement) {
37+
targetDate = new Date(targetDateString).getTime();
38+
targetElement = document.getElementById(targetElementId);
39+
if (!targetElement) {
2840
console.error("Target element not found.");
2941
return;
30-
}
31-
updateCountdown();
32-
setInterval(updateCountdown, 1000);
42+
}
43+
updateCountdown();
44+
setInterval(updateCountdown, 1000);
3345
}
34-
46+
3547
return {
36-
init: init
48+
init: init
3749
};
38-
})();
39-
50+
})();

test/index.html

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,20 @@
44
<meta charset="UTF-8">
55
<meta name="viewport" content="width=device-width, initial-scale=1.0">
66
<title>Countdown Library</title>
7+
<link rel="stylesheet" href="countdown.css">
78
</head>
89
<body>
9-
<div id="countdown"></div>
10+
11+
<div id="countdown">
12+
</div>
13+
14+
1015
<script src="countdown.js"></script>
16+
1117
<script>
1218
// Initialize the countdown with a target date and target element ID
13-
Countdown.init('2023-10-10', 'countdown');
19+
Countdown.init('2028-10-10', 'countdown');
1420
</script>
21+
1522
</body>
1623
</html>

0 commit comments

Comments
 (0)