[JS] ์‹ค์Šต-AnalogClock

์ •์„ธ์€ยท2022๋…„ 11์›” 28์ผ

training

๋ชฉ๋ก ๋ณด๊ธฐ
2/5

๐Ÿ“Œ ์•„๋‚ ๋กœ๊ทธ ์‹œ๊ณ„ ๊ตฌํ˜„

  • ํ˜„์žฌ ์‹œ๊ฐ„์„ ํ‘œ์‹œํ•˜๋Š” ์•„๋‚ ๋กœ๊ทธ ์‹œ๊ณ„๋ฅผ ๊ตฌํ˜„ํ•œ๋‹ค.
  • ์•„๋‚ ๋กœ๊ทธ ์‹œ๊ณ„๋Š” ์—ฌ๋Ÿฌ ๊ฐœ ์ฆ‰, ์žฌ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์–ด์•ผํ•œ๋‹ค.

๋‚ด๊ฐ€ ์ž‘์„ฑํ•œ ์ฝ”๋“œ๋Š” ์•„๋ž˜์™€ ๊ฐ™๋‹ค.

const SetClock = () => {

    let now = new Date();
    let nowHours = now.getHours() * 30;
    let nowMinutes = now.getMinutes() * 6;
    let nowSeconds = now.getSeconds() * 6;

    document.querySelectorAll(".hour").forEach(function (container) {
        container.style.cssText = `--deg: ${nowHours}`;
    });

    document.querySelectorAll(".minute").forEach(function (container) {
        container.style.cssText = `--deg: ${nowMinutes}`;
    });

    document.querySelectorAll(".second").forEach(function (container) {
        container.style.cssText = `--deg: ${nowSeconds}`;
    });
};

export default SetClock;

๊ฐ€์žฅ ๋จผ์ €, Date Object๋ฅผ ๊ฐ€์ ธ์™€์„œ ํ˜„์žฌ ์‹œ๊ฐ„, ๋ถ„, ์ดˆ๋ฅผ ๊ณ„์‚ฐํ•œ๋‹ค.

let now = new Date();
let nowHours = now.getHours() * 30;
let nowMinutes = now.getMinutes() * 6;
let nowSeconds = now.getSeconds() * 6;

style.cssํŒŒ์ผ์„ ์‚ดํŽด๋ณด๋ฉด ์‹œ์นจ, ๋ถ„์นจ, ์ดˆ์นจ์˜ ๊ฐ๋„๋ฅผ ํ‘œํ˜„ํ•˜๊ธฐ ์œ„ํ•ด css ๋ณ€์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜๊ณ  ์žˆ๋Š”๋ฐ, ์ด ๋ณ€์ˆ˜์˜ ๊ฐ’์„ ๋ณ€๊ฒฝํ•ด์„œ ์‹œ์นจ, ๋ถ„์นจ, ์ดˆ์นจ์˜ ๊ฐ๋„๋ฅผ ๋ณ€๊ฒฝํ•ด์ค€๋‹ค.

.analog-clock > .hand {
  --deg: 0;
  position: absolute;
  bottom: 50%;
  left: 50%;
  border: 1px solid white;
  border-top-left-radius: 10px;
  border-top-right-radius: 10px;
  transform-origin: bottom;
  transform: translate3D(-50%, 0, 0) rotate(calc(var(--deg) * 1deg));
  z-index: 10;
}

๊ทธ๋ฆฌ๊ณ  App.js ํŒŒ์ผ์—์„œ analog-clock์„ ๊ฐ€์ ธ์˜ค๊ณ  setIntervalํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•ด์„œ 1์ดˆ๋งˆ๋‹ค ํ•œ๋ฒˆ์”ฉ SetClockํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•ด์ฃผ์—ˆ๋‹ค.

import AnalogClock from './AnalogClock.js';
import SetClock from "./SetClock.js";
document.querySelectorAll('.analog-clock').forEach(AnalogClock);
setInterval(() => {
	SetClock();
}, 1000);
SetClock();

Github Repository ์ด๋™

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