Animation timing function: ease-in
Để tạo hiệu ứng animation **khởi động chậm rồi tăng tốc dần (slow start)**, sử dụng animation-timing-function: ease-in. Phù hợp cho các hiệu ứng "rơi", "trượt vào" từ ngoài màn hình.
Đặc điểm của ease-in
- Đầu: tốc độ gần 0 (chậm nhất).
- Giữa: tăng tốc đều.
- Cuối: tốc độ tối đa (ngắt quãng khi kết thúc).
Ví dụ hoàn chỉnh
<!DOCTYPE html>
<html>
<head>
<style>
div {
width: 150px;
height: 200px;
position: relative;
background-color: yellow;
animation-name: myanim;
animation-duration: 2s;
animation-direction: alternate-reverse;
animation-iteration-count: 3;
}
@keyframes myanim {
from { left: 100px; }
to { left: 200px; }
}
#demo {
animation-timing-function: ease-in;
}
</style>
</head>
<body>
<div id="demo">ease-in effect</div>
</body>
</html>
So sánh ease-in vs ease-out
| Thuộc tính | ease-in | ease-out |
|---|---|---|
| Khởi động | Chậm | Nhanh |
| Kết thúc | Nhanh (gắt) | Chậm (mượt) |
| Cảm giác | Vật thể bị kéo/push | Vật thể tự giảm tốc |
| Phù hợp | Slide-in, dropdown, modal show | Modal hide, toast dismiss, button press |
Mẹo: Kết hợp ease-in-out
Nếu muốn mượt cả đầu lẫn cuối: animation-timing-function: ease-in-out.