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

คุณสมบัติ HTML DOM Style animation


CSS ช่วยให้เราสร้างอนิเมชันสำหรับการเปลี่ยนแปลงคุณสมบัติขององค์ประกอบได้ เราใช้คุณสมบัติ animation เพื่อกำหนดสไตล์ที่ต้องการ สามารถรวมคุณสมบัติย่อย เช่น animation-name, animation-duration, animation-iteration-count เป็นต้น โดยใช้คีย์เวิร์ด animation ได้

ไวยากรณ์ (Syntax)

object.style.animation = "name duration timingFunction delay iterationCount direction fillMode playState"

ค่าที่ใช้ได้ (Values)

ค่า คำอธิบาย
animation-name ระบุชื่อ keyframe เพื่อผูกกับ selector
animation-duration ระบุระยะเวลาของอนิเมชัน (วินาที/มิลลิวินาที) สำหรับการทำงานหนึ่งรอบ
animation-timing-function ระบุโค้งความเร็วของอนิเมชัน
animation-delay ระบุหน่วงเวลาก่อนที่อนิเมชันจะเริ่ม
animation-iteration-count ระบุจำนวนครั้งที่อนิเมชันควรทำงาน
animation-direction ระบุว่าอนิเมชันควรทำงานในทิศทางปกติ ถอยหลัง หรือสลับกัน
animation-fill-mode ระบุค่าที่อนิเมชันนำไปใช้ภายนอกช่วงเวลาการทำงาน
animation-play-state ระบุว่าอนิเมชันกำลังเล่นหรือหยุดชั่วคราว
initial ตั้งค่าคุณสมบัติให้เป็นค่าเริ่มต้น
inherit สืบทอดค่าจากองค์ประกอบพาเรนต์

ตัวอย่าง (Example)

<!DOCTYPE html> <html> <head> <style> 
  div { 
    width: 5px; 
    height: 15px; 
    background-color: limegreen; 
    animation: demo 4s infinite; 
  } 
  @keyframes demo { 
    from {width: 5px; background-color: limegreen;} 
    to {width: 400px; background-color: darkgreen;} 
  } 
  @keyframes demo1 { 
    from {height: 5px; background-color: limegreen;} 
    to {height: 400px; background-color: darkgreen;} 
  } 
</style> 
<script> 
  function changeAnimation() { 
    document.getElementById("DIV1").style.animation = "demo1 4s 2"; 
  } 
</script> </head> 
<body> 
<button onclick="changeAnimation()">CHANGE ANIMATION</button> 
<p>Change the below animation by clicking the above button</p> 
<div id="DIV1"></div> 
</body> </html>

ผลลัพธ์ (Output)

ผลลัพธ์ที่ได้:

คุณสมบัติ HTML DOM Style animation

เมื่อคลิกปุ่ม CHANGE ANIMATION อนิเมชันจะเปลี่ยน:

คุณสมบัติ HTML DOM Style animation