
<script>
const hour = document.querySelector(".hour-hand");
const min = document.querySelector(".min-hand");
const second = document.querySelector(".second-hand");
function setTime() {
const seconds = new Date().getSeconds();
const secondDegree = (seconds / 60) * 360 + 90;
second.style.transform = `rotate(${secondDegree}deg)`;
const mins = new Date().getMinutes();
const minDegree = (mins / 60) * 360 + (seconds / 60) * 6 + 90;
min.style.transform = `rotate(${minDegree}deg)`;
const hours = new Date().getHours();
const hourDegree = (hours / 12) * 360 + (mins / 60) * 30 + 90; // 시간 값을 꺼내서 각도로 변경
hour.style.transform = `rotate(${hourDegree}deg)`;
}
setInterval(setTime, 1000);
setTime(); // 페이지가 열리자마자 실행되게 하기 위해서 추가
</script>
개선점
translate(): 요소를 x축, y축 방향으로 이동translateX() : 요소를 x축 방향으로 이동translateY(): 요소를 y축 방향으로 이동scale() : 요소의 크기를 x축, y축 방향으로 조정 > translate와 동일하게 x,y방향으로만 이동 가능rotate() : 요소를 회전, 단위는 degree를 사용
💡바늘(.hand)에 transform-origin이 설정되어 있지 않아서 발생
transition: all 0.05s;
transition-timing-function: cubic-bezier(0.1, 2.7, 0.58, 1);
transition-timing-function : 애니메이션 속도 변화를 어떻게 줄지 정하는 css 속성
속도 곡선
- linear : 처음부터 끝까지 같은 속도
- ease : 보통 처음엔 천천히, 중간엔 빠르고, 마지막엔 다시 천천히
- ease-in : 점점 빨라짐
- ease-out : 점점 느려짐
- cubic-bezier : 내가 원하는 대로 곡선 커스텀 가능
setInterval(setTime, 1000);
setTime();
❓왜 setInterval()이 있는데 함수 호출을 한번 더 할까
❓그러면 setInterval()를 (setTime, 0)으로 바꾸면 안될까