๐ŸŽจ new-year-countdown

kirin.logยท2021๋…„ 3์›” 20์ผ
0
post-custom-banner

๐Ÿช logic

  • html๋กœ ํ‹€์„ ์ƒ์„ฑํ•ด์ค€๋‹ค(๊ฐ’์€ js๋กœ handling์˜ˆ์ •์ž„์œผ๋กœ ์ž„์˜๋กœ ์„ค์ •ํ•ด์ค€๋‹ค)
  • css๋ฅผ ํ†ตํ•ด styling
    • ๊ฒ€์ • ํˆฌ๋ช… ๋ฐฐ๊ฒฝ์„ body::after๋กœ ์ ์šฉํ•ด์ค€๋‹ค
    • z-index๋ฅผ ํ™œ์šฉํ•˜์—ฌ text์˜ ์œ„์น˜๋ฅผ ์กฐ์ •ํ•ด์ค€๋‹ค
  • new Date()ํ•จ์ˆ˜๋กœ ํ˜„์žฌ๋…„์›”, 2022๋…„ 1์›” 1์ผ์˜ ๋‚ ์งœ ๋“ฑ์„ ์ƒ์„ฑํ•˜์—ฌ ๊ณ„์‚ฐํ•œ๋‹ค
    • 2022๋…„ 1์›” 1์ผ์—์„œ ํ˜„์žฌ ์‹œ๊ฐ์„ ๋นผ๋ฉด countdown์ด ๊ฐ€๋Šฅ
๐Ÿธ 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);
profile
boma91@gmail.com
post-custom-banner

0๊ฐœ์˜ ๋Œ“๊ธ€