π μꡬμ¬ν
- νμ¬ μκ°μ λ§κ² μκ³λ₯Ό λμμν¨λ€.
- μ΄ λ, λ°λμ ν μͺ½ λμ κ°μ΄λ°μ κ³ μ λμ΄ μκ³ λ°©ν₯μΌλ‘ μμ§μ¬μΌ νλ€.
β¨ κ²°κ³Όνλ©΄
π» μμ€μ½λ
<!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-timing-function: cubic-bezier(0.1, 2.7, 0.58, 1);
}
.hour-hand {
background: #ff8080;
}
.min-hand {
background: #ffff80;
}
.second-hand {
background: #d9d9d9;
}
</style>
<script>
const hourHand = document.querySelector(".hour-hand");
const minuteHand = document.querySelector(".min-hand");
const secondHand = document.querySelector(".second-hand");
function setDate() {
const now = new Date();
let hours = now.getHours();
const minutes = now.getMinutes();
const seconds = now.getSeconds();
const hoursDegrees = (hours / 12) * 360 + 90;
const minutesDegrees = (minutes / 60) * 360 + 90;
const secondsDegrees = (seconds / 60) * 360 + 90;
hourHand.style.transform = `rotate(${hoursDegrees}deg)`;
minuteHand.style.transform = `rotate(${minutesDegrees}deg)`;
secondHand.style.transform = `rotate(${secondsDegrees}deg)`;
console.log(hours);
}
setInterval(setDate, 1000);
</script>
</body>
</html>
π TIL
setInterval()
setInterval()μ μΌμ ν μκ° κ°κ²©μΌλ‘ νΉμ μ½λλΈλ‘μ λ°λ³΅μ μΌλ‘ μ€ννλ ν¨μμ΄λ€.
setInterval(() => {
console.log('Hello');
}, 3000);
μμ κ°μ΄ μμ±νλ©΄, 3μ΄μ ν λ²μ© Helloκ° μ½μμ μ°νκ² λλ€. μ°Έκ³ λ‘ μ§μ νλ ν¨μλ κΌ μ΅λͺ μΌ νμλ μκ³ , ν¨μμ μ΄λ¦μ λΆμ¬νκ±°λ λ€λ₯Έ κ³³μμ ν¨μλ₯Ό μ μνκ³ μ°Έμ‘°(reference)λ₯Ό μ λ¬ν μλ μλ€. λ, νμν λ§€κ°λ³μλ₯Ό μ λ¬ν μλ μλ€.
function print(string) {
console.log(`Hello ${string}!`);
}
setInterval(print, 1000, "World");
λ§μ½ μ§μ λ μκ°μ΄ κ²½κ³Όλμμ λ νΉμ μ½λλΈλ‘μ ν λ²λ§ μ€ννκ³ μΆλ€λ©΄ setTimeout()μ μ¬μ©νλ©΄ λλ€. setTimeout()μ μ¬μ©λ²μ setInterval()κ³Ό λμΌνλ€.
transform
CSSμ μμ± μ€ νλλ‘, μμμ νμ , ν¬κΈ° μ‘°μ , κΈ°μΈμ΄κΈ°, μ΄λ ν¨κ³Ό λ±μ λΆμ¬ν μ μλ€. transformμ μμμ λ³νμ μΌμΌν¬ λΏ μμ§μ΄λ λμ μΈ ν¨κ³Όλ₯Ό μ€ μ μλ€. transform μμ±μ μ¬μ©ν΄μ ν¨κ³Όλ₯Ό μ£Όκ³ , transition μμ±μ μ¬μ©ν΄μ λμ μΈ ν¨κ³Όλ₯Ό μ μ΄νλ€.
1οΈβ£ transform
transform function | μ€λͺ | λ¨μ |
translate(x, y) | μμμ μμΉλ₯Ό XμΆμΌλ‘ xλ§νΌ, YμΆμΌλ‘ yλ§νΌ μ΄λμν¨λ€. translateX(n), translateY(n)μ ν΅ν΄ XμΆ λλ YμΆμΌλ‘λ§ μ΄λμν¬ μ μλ€. |
px, %, em λ± |
scale(x, y) | μμμ ν¬κΈ°λ₯Ό XμΆμΌλ‘ xλ°°, YμΆμΌλ‘ yλ°° νλ λλ μΆμμν¨λ€. scaleX(n), scaleY(n)μ ν΅ν΄ XμΆ λλ YμΆμΌλ‘λ§ νλ λλ μΆμμν¬ μ μλ€. |
0κ³Ό μμ |
skew(x-angle, y-angle) | μμλ₯Ό XμΆμΌλ‘ x κ°λλ§νΌ, YμΆμΌλ‘ y κ°λλ§νΌ κΈ°μΈμΈλ€(λΉνΌλ€). skewX(n), skewY(n)μ ν΅ν΄ XμΆ λλ YμΆμΌλ‘λ§ κΈ°μΈμΌ(λΉν) μ μλ€. |
+/- κ°λ(deg) |
rotate(angle) | μμλ₯Ό angleλ§νΌ νμ μν¨λ€. | +/- κ°λ(deg) |
2οΈβ£ transform-origin
transform-origin μμ±μ transform μμ±κ³Ό ν¨κ» μ¬μ©λλ μμ±μΌλ‘, νμ μ€μ¬(μμ , κΈ°μ€μ )μ μ§μ νλ€. transform-originμ μ¬μ©νλ©΄ rotate(), skew() λ±μ νμ , λ³ν μμ±μ μ¬μ©νκΈ° μ μ κΈ°μ€μ μ μ§μ ν μ μλ€. μ΄κΈ° κ°μ 50% 50%μΌλ‘ ν΄λΉ μμμ μ€μ¬μ μ΄ λλ€. transform-originμ μμ± κ°μ λ°±λΆμ¨(%)κ³Ό ν€μλ μ€ νλλ‘ μ§μ ν μ μλ€.
λ°±λΆμ¨(%) | λμ© κ°λ₯ν ν€μλ |
0% | left |
0% | top |
50% | center |
100% | right |
100% | bottom |
μμ νμ λ°λΌ, transform-origin: 0% 0%, transform-origin: 100% 50% λ±μΌλ‘ λνλΌ μ μλ€. Clock μμ€μ½λμμλ, λ°λμ νμͺ½ λμ bottom rightμ λ§μΆκΈ° μν΄ λ€μκ³Ό κ°μ΄ μμ±νμλ€.
transform-origin: 100%;
transition
transitionμ CSS μμ±μ λ³κ²½ν λ μ λλ©μ΄μ μλλ₯Ό μ‘°μ νμ¬ μ λλ©μ΄μ ν¨κ³Όκ° μΌμ μκ°μ κ±Έμ³ λ€μν λ°©λ²μΌλ‘ μ§νλκ²λ νλ€.
νλ‘νΌν° | μ€λͺ | κΈ°λ³Έκ° |
transition-property | λμμ΄ λλ CSS νλ‘νΌν°λ₯Ό μ§μ νλ€. μ§μ νμ§ μλ κ²½μ°μλ λͺ¨λ νλ‘νΌν°κ° νΈλμ§μ μ λμμ΄ λλ€. 볡μμ νλ‘νΌν°λ₯Ό μ§μ νλ κ²½μ° μΌν(,)λ‘ κ΅¬λΆνλ€. λ¨, λͺ¨λ CSS νλ‘νΌν°κ° νΈλμ§μ μ λμμ΄ λ μ μλ€. (ex. display) |
all |
transition-duration | μ§μμκ°μ μ΄ λ¨μ(s) λλ λ°λ¦¬ μ΄ λ¨μ(ms)λ‘ μ§μ νλ€. transition-duration νλ‘νΌν°κ°μ transition-property νλ‘νΌν°κ°κ³Ό 1:1 λμνλ€. |
0s |
transition-timing-function | ν¨κ³Όλ₯Ό μν μμΉ ν¨μλ₯Ό μ§μ νλ€. νλΉ λ² μ΄μ§μ΄(cubic-bezier)λ₯Ό μ μνλ λ€ μ μ μν΄ μ μλλ€. 미리 μ ν΄λ 5κ°μ ν€μλκ° μ 곡λλ€. βΎ ease: μ²μ²ν μμν΄μ, λΉ λ₯΄κ² μ§νλκ³ , μ²μ²ν λλλ€. βΎ linear: μ²μκ³Ό λμ΄ κ°μ μλλ‘ μ§νλλ€. βΎ ease-in: μ²μ²ν μμνλ€. βΎ ease-out: μ²μ²ν λλλ€. βΎ ease-in-out: μ²μ²ν μμν΄μ μ²μ²ν λλλ€. |
ease |
transition-delay | λ³νν μμ κ³Ό μ€μ λ‘ μμνλ μ¬μ΄μ λκΈ°νλ μκ°μ μ΄ λ¨μ(s) λλ λ°λ¦¬ μ΄ λ¨μ(ms)λ‘ μ§μ νλ€. μ¦, νλ‘νΌν°μ κ°μ΄ λ³νν΄λ μΌμ μκ° λκΈ°ν ν νΈλμ§μ μ΄ μ€νλλλ‘ νλ€. |
0s |
transition | λͺ¨λ νΈλμ§μ
νλ‘νΌν°λ₯Ό νλ²μ μ§μ νλ€. transition: property duration function delay |
.div1 {
transition: width 1s ease-in 1s;
}
.div2 {
transition-property: width, opacity;
transition-duration: 2s, 1s;
} /* widthλ 2μ΄, opacityλ 4μ΄μ durationμ κ°λλ€. */
.div2 {
transition: width 2s, opacity 4s;
} /* transition νλ‘νΌν°λ‘ μΆμ½ ννμ΄ κ°λ₯νλ€. */
.div3 {
transition-property: width, opacity, left, top;
transition-duration: 2s, 1s;
} /* widthλ 2μ΄, opacityλ 1μ΄, leftλ 2μ΄, topμ 1μ΄μ duration */
Clock μμ€μ½λμμλ, transition-timing-functionμ cubic-bezierλ₯Ό μ΄μ©ν΄μ μκ³ λ°λμ μ λλ©μ΄μ μ μ£Όμλ€.
transition-timing-function: cubic-bezier(0.1, 2.7, 0.58, 1);
π μ°Έκ³
youtube Wes Bos
https://www.youtube.com/watch?v=xu87YWbr4X0&list=PLu8EoSxDXHP6CGK4YVJhL_VWetA865GOH&index=2
https://poiemaweb.com/css3-transform
https://www.tabmode.com/homepage/transform-origin.html
https://poiemaweb.com/css3-transition