transition은 요소의 상태 변화를 부드럽게 애니메이션하는 방법입니다.
.element {
transition: [property] [duration] [timing-function] [delay];
}
.box {
width: 100px;
height: 100px;
background: blue;
transition: width 0.5s ease-in-out, background-color 1s;
}
.box:hover {
width: 200px;
background: red;
}
@keyframes는 보다 세밀한 애니메이션을 정의할 때 사용됩니다.
@keyframes animation-name {
0% { transform: translateX(0); }
50% { transform: translateX(100px); }
100% { transform: translateX(0); }
}
.element {
animation: animation-name 2s infinite;
}
| 값 | 설명 |
|---|---|
| linear | 일정한 속도로 변화 |
| ease | 천천히 시작 → 빠르게 → 천천히 종료 (기본값) |
| ease-in | 천천히 시작 |
| ease-out | 천천히 종료 |
| ease-in-out | 천천히 시작, 천천히 종료 |
| cubic-bezier(x1, y1, x2, y2) | 사용자 정의 커브 |
.box {
transition: transform 1s cubic-bezier(0.42, 0, 0.58, 1);
}
.box {
opacity: 0;
visibility: hidden;
transition: opacity 0.5s, visibility 0.5s;
}
.box.show {
opacity: 1;
visibility: visible;
}
.menu {
max-height: 0;
overflow: hidden;
transition: max-height 0.5s ease-in-out;
}
.menu.open {
max-height: 300px;
}
| 이벤트 | 설명 |
|---|---|
| transitionstart | 트랜지션 시작 시 발생 |
| transitionend | 트랜지션 완료 시 발생 |
| animationstart | 애니메이션 시작 시 발생 |
| animationend | 애니메이션 종료 시 발생 |
| animationiteration | 애니메이션 반복 시 발생 |
const box = document.querySelector(".box");
box.addEventListener("transitionend", () => {
console.log("트랜지션 완료!");
});
const animatedElement = document.querySelector(".animated");
animatedElement.addEventListener("animationend", () => {
console.log("애니메이션 종료!");
});
animatedElement.addEventListener("animationend", (event) => {
if (event.animationName === "fadeIn") {
console.log("fadeIn 애니메이션이 끝났습니다!");
}
});