๐ช logic
body::after
๋ก ์ ์ฉํด์ค๋คz-index
๋ฅผ ํ์ฉํ์ฌ text์ ์์น๋ฅผ ์กฐ์ ํด์ค๋ค๐ธ html <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <link rel="stylesheet" href="index.css" /> <title>New Year Countdown</title> </head> <body> <div id="year" class="year"></div> <h1>New Year Countdown</h1> <div id="countdown" class="countdown"> <!--days--> <div class="time"> <h2 id="days">00</h2> <small>days</small> </div> <!--hours--> <div class="time"> <h2 id="hours">00</h2> <small>hours</small> </div> <!--minutes--> <div class="time"> <h2 id="minutes">00</h2> <small>minutes</small> </div> <!--seconds--> <div class="time"> <h2 id="seconds">00</h2> <small>seconds</small> </div> </div> <!--countdownํ๊ธฐ ์ง์ ๋ณด์ฌ์ง๋ gif--> <img src="./img/spinner.gif" alt="Loading..." id="loading" class="loading" /> <script src="index.js"></script> </body> </html>
๐ธ css @import url('https://fonts.googleapis.com/css?family=Lato&display=swap'); * { box-sizing: border-box; } body { display: flex; flex-direction: column; align-items: center; justify-content: center; margin: 0; color: #fff; background-image: url('https://images.unsplash.com/photo-1467810563316-b5476525c0f9?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=1349&q=80'); background-repeat: no-repeat; background-size: cover; background-position: center center; height: 100vh; font-family: 'Lato', sans-serif; text-align: center; overflow: hidden; } /* Add a dark overlay */ body::after { content: ''; position: absolute; top: 0; left: 0; width: 100%; height: 100%; background-color: rgba(0, 0, 0, 0.5); } body * { z-index: 1; /* ๊ฒ์ ๋ฐฐ๊ฒฝ์ด ๋ค๋ก ๊น๋ฆฌ๋๋ก */ } h1 { font-size: 60px; margin: -80px 0 40px; } .year { font-size: 200px; z-index: -1; opacity: 0.2; position: absolute; bottom: 20px; left: 50%; transform: translateX(-50%); } .countdown { /* display: flex; */ display: none; transform: scale(2); } .time { display: flex; flex-direction: column; align-items: center; justify-content: center; margin: 15px; } .time h2 { margin: 0 0 5px; } @media (max-width: 500px) { h1 { font-size: 45px; } .time { margin: 5px; } .time h2 { font-size: 12px; margin: 0; } .time small { font-size: 10px; } }
๐ธ js const days = document.getElementById('days'); const hours = document.getElementById('hours'); const minutes = document.getElementById('minutes'); const seconds = document.getElementById('seconds'); const countdown = document.getElementById('countdown'); const year = document.getElementById('year'); const loading = document.getElementById('loading'); const currentYear = new Date().getFullYear(); const newYearTime = new Date(`January 01 ${currentYear + 1} 00:00:00`); // console.log(newYearTime); Sat Jan 01 2022 00:00:00 // Set background year year.innerText = currentYear + 1; // 2022 // Update countdown time function updateCountdown() { const currentTime = new Date(); // console.log(currentTime) Tue Mar 16 2021 10:03:53(ํ์ฌ์๊ฐ ์ถ๋ ฅ) const diff = newYearTime - currentTime; const d = Math.floor(diff / 1000 / 60 / 60 / 24); const h = Math.floor(diff / 1000 / 60 / 60) % 24; const m = Math.floor(diff / 1000 / 60) % 60; const s = Math.floor(diff / 1000) % 60; // Add values to DOM days.innerHTML = d; hours.innerHTML = h < 10 ? '0' + h : h; minutes.innerHTML = m < 10 ? '0' + m : m; seconds.innerHTML = s < 10 ? '0' + s : s; } // Show spinner before countdown setTimeout(() => { loading.remove(); countdown.style.display = 'flex'; }, 1000); // Run every second setInterval(updateCountdown, 1000);