심화 동적 효과, Animation
transition이 두 상태 사이의 전환이라면, animation은 시작부터 끝까지 직접 정의하는 동적 효과이다.
@keyframes로 애니메이션의 흐름을 정의하고, animation 속성으로 요소에 적용한다.
@keyframes
애니메이션의 각 단계를 정의한다.
/* from / to — 시작과 끝 2단계 */
@keyframes fadeIn {
from { opacity: 0; }
to { opacity: 1; }
}/* 퍼센트 — 세밀한 단계 제어 */
@keyframes bounce {
0% { transform: translateY(0); }
50% { transform: translateY(-30px); }
100% { transform: translateY(0); }
}animation 속성
① 개별 속성
.box {
animation-name: fadeIn; /* @keyframes 이름 */
animation-duration: 0.5s; /* 한 사이클 시간 */
animation-timing-function: ease; /* 속도 곡선 */
animation-delay: 0s; /* 시작 전 대기 시간 */
animation-iteration-count: 1; /* 반복 횟수 */
animation-direction: normal; /* 재생 방향 */
animation-fill-mode: none; /* 시작·끝 상태 유지 여부 */
animation-play-state: running; /* 재생·일시정지 */
}animation-iteration-count — 반복 횟수를 지정한다.
| 값 | 설명 |
|---|---|
1 | 1회 재생 (기본값) |
n | n회 반복 |
infinite | 무한 반복 |
animation-direction — 재생 방향을 지정한다.
| 값 | 설명 |
|---|---|
normal | 정방향 (기본값) |
reverse | 역방향 |
alternate | 정방향 → 역방향 번갈아 |
alternate-reverse | 역방향 → 정방향 번갈아 |
animation-fill-mode — 시작 전·종료 후 요소의 상태를 지정한다.
| 값 | 설명 |
|---|---|
none | 원래 상태로 돌아감 (기본값) |
forwards | 종료 후 마지막 keyframe 상태 유지 |
backwards | 시작 전 첫 keyframe 상태 적용 |
both | forwards + backwards 둘 다 |
② 단축 속성
name duration timing-function delay iteration-count direction fill-mode 순서로 작성한다.
.box {
animation: fadeIn 0.5s ease 0s 1 normal forwards;
}Transition vs Animation
| Transition | Animation | |
|---|---|---|
| 트리거 | 상태 변화 필요 (hover 등) | 자동 실행 가능 |
| 단계 | 시작 → 끝 2단계 | @keyframes로 세밀하게 제어 |
| 반복 | 불가 | infinite 등 반복 가능 |
| 적합한 경우 | hover, focus 등 인터랙션 | 로딩, 배너, 주목 효과 |
마지막 수정