Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> CSS

คุณสมบัติ CSS animation-duration

ใช้คุณสมบัติ animation-duration เพื่อกำหนดว่าแอนิเมชันควรใช้เวลานานเท่าไรในการทำหนึ่งรอบ (cycle)

ตัวอย่างโค้ด

ลองรันโค้ดด้านล่างเพื่อทดสอบ animation-duration property:

<!DOCTYPE html>
<html>
<head>
  <style>
    div {
      width: 150px;
      height: 200px;
      position: relative;
      background: red;
      animation-name: myanim;
      animation-duration: 2s;
    }
    @keyframes myanim {
      from { left: 0px; background-color: green; }
      to { left: 200px; background-color: blue; }
    }
  </style>
</head>
</body>
  <div></div>
</body>
</html>

คำอธิบาย: animation-duration: 2s ทำให้แอนิเมชันจาก from (ซ้าย 0px, สีเขียว) ไป to (ซ้าย 200px, สีน้ำเงิน) ใช้เวลา 2 วินาที ต้องมี animation-name อ้างอิง @keyframes ด้วย