หากต้องการสร้างภาพเคลื่อนไหวคีย์เฟรมใน CSS3 ให้กำหนดคีย์เฟรมแต่ละรายการ คีย์เฟรมจะควบคุมขั้นตอนของแอนิเมชั่นขั้นกลางใน CSS3
ต่อไปนี้เป็นรหัสสำหรับกำหนดคีย์เฟรมใน CSS3 -
ตัวอย่าง
<!DOCTYPE html>
<html>
<head>
<style>
body {
font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif;
}
div {
width: 100px;
height: 100px;
background: red;
position: relative;
animation: colorChange 5s infinite;
}
@keyframes colorChange {
from {
background: red;
left: 0px;
}
to {
background: rgb(32, 229, 255);
left: 300px;
}
}
</style>
</head>
<body>
<h1>Defining keyframes in CSS</h1>
<div></div>
<h2>The above square will change its color and position with time</h2>
</body>
</html> ผลลัพธ์
รหัสข้างต้นจะสร้างผลลัพธ์ต่อไปนี้ -

หลังจาก 5 วินาที ตำแหน่งและสีจะเปลี่ยนดังนี้ −
