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

คุณสมบัติ CSS สำหรับตั้งค่าโค้งความเร็วของ Animation

ใช้คุณสมบัติ animation-timing-function เพื่อตั้งค่าโค้งความเร็วของแอนิเมชัน

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

ลองรันโค้ดด้านล่างเพื่อตั้งค่า ease และ ease-in animation effect:

<!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; }
    }
    #demo1 { animation-timing-function: ease; }
    #demo2 { animation-timing-function: ease-in; }
  </style>
</head>
<body>
  <div id="demo1">ease effect</div>
  <div id="demo2">ease-in effect</div>
</body>
</html>

คำอธิบาย: เปรียบเทียบ ease (เริ่มช้า เร็วกลาง จบช้า) กับ ease-in (เริ่มช้า แล้วเร็วขึ้น) ทั้งคู่ใช้ keyframes เดียวกัน alternate-reverse ทำให้รันถอยหลังก่อน