본문 바로가기

Develop/기타

JavaScript 30 - DAY 02 JS and CSS Clock

 

JavaScript 30

Build 30 things with vanilla JS in 30 days with 30 tutorials

javascript30.com

 

ajyng/javascript-study

Contribute to ajyng/javascript-study development by creating an account on GitHub.

github.com

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>JS + CSS Clock</title>
</head>
<body>


    <div class="clock">
      <div class="clock-face">
        <div class="hand hour-hand"></div>
        <div class="hand min-hand"></div>
        <div class="hand second-hand"></div>
      </div>
    </div>


  <style>
    html {
      background: #018DED url(https://unsplash.it/1500/1000?image=881&blur=5);
      background-size: cover;
      font-family: 'helvetica neue';
      text-align: center;
      font-size: 10px;
    }

    body {
      margin: 0;
      font-size: 2rem;
      display: flex;
      flex: 1;
      min-height: 100vh;
      align-items: center;
    }

    .clock {
      width: 30rem;
      height: 30rem;
      border: 20px solid white;
      border-radius: 50%;
      margin: 50px auto;
      position: relative;
      padding: 2rem;
      box-shadow:
        0 0 0 4px rgba(0,0,0,0.1),
        inset 0 0 0 3px #EFEFEF,
        inset 0 0 10px black,
        0 0 10px rgba(0,0,0,0.2);
    }

    .clock-face {
      position: relative;
      width: 100%;
      height: 100%;
      transform: translateY(-3px); /* account for the height of the clock hands */
    }

    .hand {
      width: 50%;
      height: 6px;
      background: black;
      position: absolute;
      top: 50%;
      transform: rotate(90deg);
      transform-origin: 100%;
      transition: all 0.05s;
      transition-timing-function: ease;
    }

  </style>

  <script>
    const secondHand = document.querySelector('.second-hand');
    const minHand = document.querySelector('.min-hand');
    const hourHand = document.querySelector('.hour-hand');

    function setDate() {
      const now = new Date();

      const second = now.getSeconds();
      const secondDegrees = (second / 60) * 360 + 90;

      const min = now.getMinutes();
      const minDegrees = ((min / 60) * 360) + ((second/60)*6) + 90;

      const hour = now.getHours();
      const hourDegrees = ((hour / 12) * 360) + ((min/60)*30) + 90;

      secondHand.style.transform = `rotate(${secondDegrees}deg)`;
      minHand.style.transform = `rotate(${minDegrees}deg)`;
      hourHand.style.transform = `rotate(${hourDegrees}deg)`;
    }

    setInterval(setDate, 1000);
     
  </script>
</body>
</html>

목표


아날로그 시계 만들기

코드 분석


const secondHand = document.querySelector('.second-hand');
const minHand = document.querySelector('.min-hand');
const hourHand = document.querySelector('.hour-hand');
  • 시계침들을 선택한다.
function setDate() {
	const now = new Date();
	
	const second = now.getSeconds();
	const secondDegrees = (second / 60) * 360 + 90;
	
	const min = now.getMinutes();
	const minDegrees = ((min / 60) * 360) + ((second/60)*6) + 90;
	
	const hour = now.getHours();
	const hourDegrees = ((hour / 12) * 360) + ((min/60)*30) + 90;
	
	secondHand.style.transform = `rotate(${secondDegrees}deg)`;
	minHand.style.transform = `rotate(${minDegrees}deg)`;
	hourHand.style.transform = `rotate(${hourDegrees}deg)`;
}
  • Date 객체 생성을 통해 현재 날짜를 가져온다. 생성된 객체로부터 시/분/초를 구할 수 있다.
  • 시계침들이 움직일 각도를 구해준다. (과정은 생략,, 알아서들 구해보시길)
  • 각 시계침들의 transform 속성에 rotate(??deg)를 추가해줘서 시계침을 회전시킨다.
setInterval(setDate, 1000);
  • 위에서 구현한 setDate 함수를 1초(1000ms)마다 반복해서 실행한다.

추가로 공부한 내용


    .hand {
      width: 50%;
      height: 6px;
      background: black;
      position: absolute;
      top: 50%;
      transform: rotate(90deg);
      transform-origin: 100%;
      transition: all 0.05s;
      transition-timing-function: ease;
    }
  • transform 속성은 HTML 엘리먼트의 모양, 크기, 위치 같은 걸 이러쿵 저러쿵 바꿀 수 있다.
  • transform-origin 속성을 통해 기준점을 원하는대로 옮길 수 있다. (원래는 중점을 기준으로 동작!)
  • transition 속성을 사용하여 정해진 시간 동안 엘리먼트의 속성값을 부드럽게 변화시킬 수 있다.
  • transition-timing-function 속성은 효과가 일어나는 속도를 조절할 수 있다.