javascript/javascript30
[javascript30]CSS + JS Clock
소영
2021. 7. 5. 21:02
javascript css+js Clock 설명
css에서 transform 과 javascript setInterval로 아날로그 시계를 만들면된다.
1.transform 으로 중심을 고정시키고 시,분,초만 각도를 조절할 수 있게 하기
2.setInterval로 시계 자동으로 움직이게 하기
3. new Date를 이용해 현재 시간을 불러와, 각도에 맞춰 계산하여 transform에 넣기
코드
<!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-origin: 100%;
transform: rotate(90deg);
transition: all 0.05s;
transition-timing-function: cubic-bezier(0.1, 2.7, 0.58, 1);
}
</style>
<script>
const secondHand = document.querySelector('.second-hand');
const minsHand = document.querySelector('.min-hand');
const hourHand = document.querySelector('.hour-hand');
function setDate() {
const now = new Date();
const seconds = now.getSeconds();
//현재 초을 전체 초(60)를 나누고 전체 각도(360)를 곱한다(90도를 더해주는 것은 처음 -90도로 시작되기 때문)
const secondsDegrees = ((seconds / 60) * 360) + 90;
secondHand.style.transform = `rotate(${secondsDegrees}deg)`;
const mins = now.getMinutes();
const minsDegrees = ((mins / 60) * 360) + ((seconds/60)*6) + 90;
minsHand.style.transform = `rotate(${minsDegrees}deg)`;
const hour = now.getHours();
const hourDegrees = ((hour / 12) * 360) + ((mins/60)*30) + 90;
hourHand.style.transform = `rotate(${hourDegrees}deg)`;
}
setInterval(setDate, 1000);
setDate();
</script>
</body>
</html>
음 근데 왜 강의 코드랑 github에 올라가 있는 코드가 좀 많이 다른지 잘 모르겠다 특히 계산부분 ;;;;;
알게 된 문법
CSS
transform-origin
transform 속성과 함께 사용되며 회전 축(중심)을 지정한다.
속성 값
left | 0% |
center | 50% |
right | 100% |
top | 0% |
bottom | 100% |
transition-timing-function
가속 곡선을 설정하여 전환 속도가 지속 시간에 따라 달라질 수 있도록 한다.(MDN 참조)
(시계가 틱틱하고 움직이게 하기위해 속도를 느렸다가 빠르게? 하려고 사용한 css)
JAVASCRIPT
new Date()
현재 시간을 반환한다.
new Date와 쓰이는 메서드
getDate() | 현지 시간 기준 일(1-31)일(하루) |
getDay() | 현지 시간 기준 요일(0-6) |
getFullYear() | 현지 시간 기준 연도(네 자리 연도면 네자리로) |
getHours() | 현지 시간 기준 시(0-23) |
getMinutes() | 현지 시간 기준 분(0-999) |
getMonth() | 현지 시간 기준 월(0-11) |
getSeconds() | 현지 시간 기준 초(0-59) |
여기까지는 이해는 되는데
const minsDegrees = ((mins / 60) * 360) + ((seconds/60)*6) + 90;
const hourDegrees = ((hour / 12) * 360) + ((mins/60)*30) + 90;
시와 분을 왜 이렇게 나누는지 잘 모르겠다
알려주실 분 찾아요......