728x90
기본값과 결과를 html로 띄어놓고 코딩해보세요
1. 기본값
<!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%;
}
</style>
<script>
</script>
</body>
</html>
2. 결과
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>JS + CSS Clock</title>
<script
src="https://code.jquery.com/jquery-3.6.0.min.js"
integrity="sha256-/xUj+3OJU5yExlq6GSYGSHk7tPXikynS7ogEvDej/m4="
crossorigin="anonymous"
></script>
</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%;
}
.second-hand {
transition: linear 1s;
}
</style>
<script>
const hourHand = document.querySelector(".hour-hand");
const minHand = document.querySelector(".min-hand");
const secHand = document.querySelector(".second-hand");
let Current = (function () {
function Current() {
this.hour = new Date().getHours() + 3;
this.min = new Date().getMinutes() + 15;
this.sec = new Date().getSeconds() + 15;
}
return Current;
})();
setInterval(() => {
let clock = new Current();
let hourdeg =
clock.hour * 30 + (clock.min / 60) * 30 + (clock.sec / 3600) * 30;
hourHand.style.transform = `rotate(${hourdeg}deg)`;
hourHand.style.transformOrigin = `right`;
let mindeg = clock.min * 6 + (clock.sec / 60) * 6;
minHand.style.transform = `rotate(${mindeg}deg)`;
minHand.style.transformOrigin = `right`;
let secdeg = clock.sec * 6;
secHand.style.transform = `rotate(${secdeg}deg)`;
secHand.style.transformOrigin = `right`;
}, 1000);
</script>
</body>
</html>
3. 리뷰
처음 시침,분침,초침이 9시방향을 가리키고 있어서 12시방향으로 옮겨주기 위해 3시간, 15분,15초 씩 더해줬다.
setInterval 로 1초마다 한번씩 값을 갱신하도록 작성했다.
초침은 60초에 360도를 움직이므로 1초에 6도 움직인다.
분침은 60분에 360도를 움직이므로 1분에 6도 움직인다.
여기에 60초에 1분이므로 60초에 6도움직이기 때문에 초/60 *6 을 추가한다.
시침은 12시간에 360도를 움직이므로 1시간 30도, 60분에 30도움직이고, 3600초에 30도 움직이므로 값을 추가한다.
728x90
'개발 > html, css, js' 카테고리의 다른 글
[30일 챌린지 Day-4] 배열,객체 알고리즘 (0) | 2022.07.25 |
---|---|
[30일 챌린지 Day-3] input값 적용하기 (0) | 2022.07.25 |
[JavaScript] 농구게임 만들기 (0) | 2022.06.21 |
[HTML, JavaScript] 정규표현식 (0) | 2022.06.21 |
[JS, HTML, CSS] 이벤트 (0) | 2022.06.17 |