Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> CSS

Thiết lập animation khởi động chậm (slow start) với CSS

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ínhease-inease-out
Khởi độngChậmNhanh
Kết thúcNhanh (gắt)Chậm (mượt)
Cảm giácVật thể bị kéo/pushVật thể tự giảm tốc
Phù hợpSlide-in, dropdown, modal showModal 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.