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

คุณสมบัติ CSS สำหรับรัน Animation แบบสลับทิศทาง (Alternate)

ใช้คุณสมบัติ animation-direction เพื่อรันแอนิเมชันในทิศทางสลับกัน ค่าที่ใช้คือ alternate

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

<!DOCTYPE html>
<html>
<head>
  <style>
    div {
      width: 150px;
      height: 200px;
      background-color: yellow;
      animation-name: myanim;
      animation-duration: 2s;
      animation-direction: alternate;
      animation-iteration-count: 3;
    }
    @keyframes myanim {
      from { background-color: green; }
      to { background-color: blue; }
    }
  </style>
</head>
<body>
  <div></div>
</body>
</html>

คำอธิบาย: animation-direction: alternate ทำให้แอนิเมชันรันไป (จากเขียวเป็นน้ำเงิน) แล้วรันกลับ (จากน้ำเงินเป็นเขียว) สลับกันไปเรื่อยๆ animation-iteration-count: 3 รัน 3 รอบ (รวมไป-กลับ)