애니메이션(Animation): 연속된 이미지를 빠르게 보여줘서 움직이는 것처럼 보이게 하는 기법
CSS에서는 요소에 움직임, 변화 효과를 부여할 수 있음
1) transition
속성 활용
.box:hover {
transition: all 0.5s ease-in-out;
background-color: blue;
}
2) animation
+ @keyframes
활용
@keyframes
란?애니메이션의 중간 상태를 정의하는 규칙
문법:
@keyframes moveRight {
from {
left: 0;
}
to {
left: 200px;
}
}
진행도(%)로도 작성 가능:
@keyframes move {
0% {
left: 0;
}
50% {
left: 200px;
}
100% {
top: 200px;
left: 200px;
}
}
속성 | 설명 | 예제 |
---|---|---|
animation-name | 적용할 keyframes 이름 지정 | animation-name: move; |
animation-duration | 애니메이션 재생 시간 설정 | animation-duration: 2s; |
animation-direction | 재생 방향 설정 (정방향/역방향 등) | animation-direction: alternate; |
animation-iteration-count | 반복 횟수 설정 (infinite 가능) | animation-iteration-count: 3; |
animation-timing-function | 애니메이션의 속도 곡선 정의 | animation-timing-function: ease-in-out; |
animation-delay | 시작 지연 시간 설정 | animation-delay: 1s; |
여러 속성을 한 줄로 작성 가능:
animation: move 2s ease-in-out 1s infinite alternate;
animation-name
→ animation-duration
→ animation-timing-function
→ animation-delay
→ animation-iteration-count
→ animation-direction
❗️ 주의: 순서가 틀리면 예상과 다른 동작이 발생할 수 있음
@keyframes slide {
from {
transform: translateX(0);
}
to {
transform: translateX(200px);
}
}
.box {
width: 100px;
height: 100px;
background: coral;
animation: slide 3s ease-in-out infinite alternate;
}
.box
요소가 좌우로 왔다 갔다 반복 움직임transition
사용 @keyframes
+ animation
사용