
๋ด๊ฐ ์์ฑํ ์ฝ๋๋ ์๋์ ๊ฐ๋ค.
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();