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

Hiệu ứng Light Speed In (Tốc độ ánh sáng vào) với CSS


Hiệu ứng Light Speed In

Hiệu ứng Light Speed In là cặp đôi của lightSpeedOut - phần tử **bay vào từ phải** với hiệu ứng nghiêng (skewX) ngược chiều, giảm tốc (ease-out), thời gian 0.5s.

Keyframes

@keyframes lightSpeedIn {
  0% {
    transform: translateX(100%) skewX(-30deg); /* Bắt đầu từ phải, nghiêng */
    opacity: 0;
  }
  60% {
    transform: translateX(-20%) skewX(30deg); /* Vượt qua vị trí, nghiêng ngược */
    opacity: 1;
  }
  80% {
    transform: translateX(0%) skewX(-15deg);  /* Nảy ngược nhẹ */
    opacity: 1;
  }
  100% {
    transform: translateX(0%) skewX(0deg);    /* Về vị trí chính xác */
    opacity: 1;
  }
}

/* Timing function */
.lightSpeedIn {
  animation-timing-function: ease-out; /* Giảm tốc dần */
  animation-duration: 0.5s;            /* Nhanh nhưng chậm hơn Out */
}

Ví dụ hoàn chỉnh

<!DOCTYPE html>
<html>
<head>
  <style>
    .animated {
      width: 100px; height: 100px;
      background: #2196F3;
      animation-fill-mode: both;
    }
    @keyframes lightSpeedIn {
      0% { transform: translateX(100%) skewX(-30deg); opacity: 0; }
      60% { transform: translateX(-20%) skewX(30deg); opacity: 1; }
      80% { transform: translateX(0%) skewX(-15deg); opacity: 1; }
      100% { transform: translateX(0%) skewX(0deg); opacity: 1; }
    }
    .lightSpeedIn {
      animation-name: lightSpeedIn;
      animation-timing-function: ease-out;
      animation-duration: 0.5s;
    }
  </style>
</head>
<body>
  <div class="animated lightSpeedIn"></div>
  <button onclick="location.reload()">Chạy lại</button>
</body>
</html>

So sánh In vs Out

Đặc điểm Light Speed In Light Speed Out
Hướng Từ phải vào (translateX: 100% → 0) Từ giữa ra phải (translateX: 0 → 100%)
SkewX -30deg → 30deg → -15deg → 0 0 → -30deg
Timing ease-out (giảm tốc) ease-in (tăng tốc)
Duration 0.5s 0.25s
Opacity 0 → 1 1 → 0