-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
397 lines (354 loc) · 9.72 KB
/
index.html
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>JavaScript Countdown Timer Tutorial</title>
<style>
body {
margin: 0;
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto,
'Helvetica Neue', Arial, sans-serif;
font-size: 16px;
background-color: #121212;
color: #ffffff;
display: flex;
justify-content: center;
align-items: center;
min-height: 100vh;
line-height: 1.6;
}
main {
max-width: 768px;
width: 100%;
padding: 20px;
box-sizing: border-box;
}
h1 {
font-size: 2.5rem;
}
h2 {
margin-top: 20px;
font-size: 1.5rem;
}
p {
margin-top: 10px;
font-size: 1rem;
}
img {
max-width: 100%;
margin-top: 20px;
}
a {
color: #ff3367;
}
ul {
padding-left: 20px;
margin: 10px 0;
}
ul li {
margin-bottom: 5px;
}
pre > pre {
background-color: #1e1e1e;
padding: 20px;
border-radius: 5px;
overflow-x: auto;
font-size: 0.9rem;
}
code {
background-color: #1e1e1e;
color: #dcdcdc;
}
:not(pre) > code {
padding: 2px 5px;
border-radius: 3px;
}
</style>
</head>
<body>
<main>
<h1>JavaScript Countdown Timer</h1>
<p>
A countdown timer tracks and displays the time remaining until a
specific event or deadline. From building excitement for the New Year to
creating urgency during limited-time promotions, countdown timers are a
powerful way to engage users and drive action.
</p>
<p>
In this tutorial, you'll learn how to build a countdown timer for the
New Year. The timer will be reusable and customizable, so you can easily
adapt it for other events and add dynamic features to your web projects.
</p>
<h2>Step 1: Set Up the Project Structure</h2>
<p>
Open
<a
href="https://codesandbox.io/p/sandbox/github/codesandbox/sandbox-templates/tree/main/html-css"
target="_blank"
rel="noopener noreferrer"
>the CodeSandbox HTML + CSS template</a
>
and click <strong>Sign In</strong> at the top right to edit it. Sign in
with GitHub, Google or Apple.
</p>
<p>
Create two JavaScript files in the left Explorer sidebar by
right-clicking on the background and selecting
<strong>New File</strong>:
</p>
<ul>
<li><code>app.js</code></li>
<li><code>countdown.js</code></li>
</ul>
<p>
Finally, download the image below and drag and drop it into the
explorer, with the name <code>fireworks.avif</code>.
</p>
<img
src="fireworks.avif"
alt="Fireworks background image for the countdown timer"
/>
<h2>Step 2: Replace content in <code>index.html</code></h2>
<p>
Replace all content in the HTML file <code>index.html</code>, to create
the foundation for the countdown timer:
</p>
<pre class="code-block lang-html">
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Countdown Timer</title>
<link rel="stylesheet" href="/styles.css">
</head>
<body>
<h1>New Year Countdown</h1>
<div class="countdown-timer"></div>
<div class="message"></div>
<div class="year"></div>
<script src="/app.js" type="module"></script>
</body>
</html></pre
>
<h2>Step 3: Develop the countdown JavaScript</h2>
<p>
Create a <code>createCountdown</code> function in
<code>countdown.js</code>:
</p>
<pre class="code-block lang-js">
export function createCountdown(targetDate, onUpdate, onComplete) {
const currentTime = new Date().getTime();
let timeRemaining = targetDate.getTime() - currentTime;
let intervalId;
function tick() {
timeRemaining -= 1000;
if (timeRemaining <= 0) {
onComplete();
clearInterval(intervalId);
} else {
onUpdate({
days: Math.floor(timeRemaining / 1000 / 60 / 60 / 24),
hours: Math.floor(timeRemaining / 1000 / 60 / 60) % 24,
minutes: Math.floor(timeRemaining / 1000 / 60) % 60,
seconds: Math.floor(timeRemaining / 1000) % 60,
});
}
}
tick();
setInterval(tick, 1000);
}
</pre
>
<h2>Step 4: Initialize the countdown</h2>
<p>In <code>app.js</code>, initialize and manage the countdown:</p>
<pre class="code-block lang-js">
import confetti from 'https://esm.sh/[email protected]';
import { createCountdown } from './countdown.js';
const countdownElement = document.querySelector('.countdown-timer');
const messageElement = document.querySelector('.message');
const yearElement = document.querySelector('.year');
const currentYear = new Date().getFullYear();
const targetDate = new Date(`1 Jan ${
// If it's the first 10 minutes of the new year, set the target
// year to the current year, to be able to see the celebration
new Date().getMonth() === 0 && new Date().getDate() === 1 && new Date().getHours() === 0 && new Date().getMinutes() < 10
? currentYear
: currentYear + 1
} 00:00:00`);
const countdown = createCountdown(
targetDate,
function onUpdate({ days, hours, minutes, seconds }) {
countdownElement.innerHTML = `
<div class="timer">
<span>${days}</span> days
</div>
<div class="timer">
<span>${hours}</span> hours
</div>
<div class="timer">
<span>${minutes}</span> minutes
</div>
<div class="timer">
<span>${seconds}</span> seconds
</div>
`;
},
function onComplete() {
confetti({
particleCount: 200,
angle: 60,
spread: 55,
origin: { x: 0 },
});
confetti({
particleCount: 200,
angle: 120,
spread: 55,
origin: { x: 1 },
});
countdownElement.style.display = 'none';
messageElement.textContent = 'Happy New Year!';
yearElement.textContent = currentYear + 1;
}
);</pre>
<h2>Step 5: Style the countdown timer</h2>
<p>Replace all content <code>styles.css</code> with new styles:</p>
<pre class="code-block lang-css">
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto,
"Helvetica Neue", Arial, sans-serif;
font-size: 16px;
color: #fff;
line-height: 1.8;
position: relative;
background: #000;
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
min-height: 100vh;
margin: 0;
}
body::before {
content: "";
position: absolute;
top: 0;
right: 0;
bottom: 0;
left: 0;
background-image: url("/fireworks.avif");
background-repeat: no-repeat;
background-size: cover;
background-position: center;
opacity: 0.2; /* Adjusted to 20% opacity */
z-index: -1;
}
h1 {
font-size: 3.75rem; /* 60px */
margin: 20px 0 40px;
text-align: center;
}
.countdown-timer {
display: flex;
gap: 20px;
justify-content: center;
align-items: flex-start; /* Align blocks to the top */
flex-wrap: wrap;
}
.timer {
display: flex;
flex-direction: column;
align-items: center;
text-align: center;
margin-top: 5px;
font-size: 1rem;
text-transform: uppercase;
}
.timer span {
margin: 0;
font-size: 4rem; /* Adjusted size for the count */
}
.message {
font-size: 4rem; /* Adjusted for clarity */
font-weight: bold;
text-align: center;
}
.year {
font-size: 12.5rem; /* 200px */
z-index: -1;
opacity: 0.2;
position: absolute;
bottom: 0;
left: 50%;
transform: translateX(-50%);
margin: 0;
}
@media (max-width: 500px) {
h1 {
font-size: 1.375rem; /* 22px */
}
.countdown-timer {
gap: 10px;
font-size: 12px;
}
.timer span {
font-size: 1.875rem; /* 30px */
}
.year {
font-size: 6.25rem; /* 100px */
}
.message {
font-size: 1.563rem; /* 25px */
}
}
</pre
>
<p>
You're done! The preview URL at the top right of the CodeSandbox editor
(ending in <code>csb.app/</code>) is your website.
</p>
<h2>Step 6 (stretch goal): Deploy to Netlify</h2>
<p>
CodeSandbox URLs can sometimes stop working. To work around this,
deploying to another platform like Netlify is an option.
</p>
<p>
Do this by clicking on the
<strong>Download</strong>
button at the top left of CodeSandbox and then dragging the downloaded
ZIP file into
<a
href="https://app.netlify.com/drop"
target="_blank"
rel="noopener noreferrer"
>Netlify Drop</a
>
</p>
<script type="module">
import { codeToHtml } from 'https://esm.sh/[email protected]';
document.querySelectorAll('.code-block').forEach(async (block) => {
const langClass = Array.from(block.classList).find((cls) =>
cls.startsWith('lang-'),
);
const lang = langClass ? langClass.replace('lang-', '') : 'txt';
let code = block.innerHTML.trim();
code = code
.replace(/</g, '<')
.replace(/>/g, '>')
.replace(/&/g, '&');
block.innerHTML = await codeToHtml(code, {
lang,
theme: 'dark-plus',
});
});
</script>
</main>
</body>
</html>