คุณสมบัติ transition ของ Style ใน HTML DOM ใช้สำหรับอ่านหรือตั้งค่าของคุณสมบัติ CSS transition แบบ shorthand ของเอลิเมนต์ HTML
รูปแบบการใช้งาน (Syntax)
1. อ่านค่า transition
object.transition
2. ตั้งค่า transition
object.transition = "value"
ค่าที่เป็นไปได้ (Value) มีดังนี้:
| ค่า | คำอธิบาย |
|---|---|
| initial | ตั้งค่าคุณสมบัติเป็นค่าเริ่มต้น |
| inherit | สืบทอดค่าจากเอลิเมนต์พาเรนต์ |
| property duration timing-function delay | property = ชื่อคุณสมบัติ CSS ที่จะ transition, timing-function = ความเร็วของ effect, delay = เวลาล่าช้าก่อนเริ่ม effect |
ตัวอย่างการใช้งาน
<!DOCTYPE html>
<html>
<style>
body {
color: #000;
height: 100vh;
}
.btn {
background: #db133a;
border: none;
height: 2rem;
border-radius: 2px;
width: 40%;
display: block;
color: #fff;
outline: none;
cursor: pointer;
margin: 1rem 0;
}
.circle {
height: 100px;
width: 100px;
background-color: #db133a;
}
.circle:hover {
height: 200px;
width: 200px;
border-radius: 50%;
}
.show {
font-size: 1.2rem;
margin: 1rem 0;
}
</style>
<body>
<h1>ตัวอย่างคุณสมบัติ transition ของ Style</h1>
<div class='circle'></div>
<button onclick="set()" class="btn">ตั้งค่า Transition</button>
<div class="show">ตอนนี้ลอง hover ที่สี่เหลี่ยมดู</div>
<script>
function set() {
document.querySelector('.circle').style.transition = "all 1s";
}
</script>
</body>
</html>
ผลลัพธ์
คลิกที่ปุ่ม "ตั้งค่า Transition" แล้วลอง hover ที่ "สี่เหลี่ยมสีแดง" เพื่อดูเอฟเฟกต์ transition

