คุณสมบัติ transitionProperty ของ Style ใน HTML DOM ใช้สำหรับอ่านหรือตั้งค่าชื่อของคุณสมบัติ CSS ของเอลิเมนต์ที่ transition effect จะใช้งาน
รูปแบบการใช้งาน (Syntax)
1. อ่านค่า transitionProperty
object.transitionProperty
2. ตั้งค่า transitionProperty
object.transitionProperty = "value"
ค่าที่เป็นไปได้ (Value) มีดังนี้:
| ค่า | คำอธิบาย |
|---|---|
| initial | ตั้งค่าคุณสมบัติเป็นค่าเริ่มต้น |
| inherit | สืบทอดค่าจากเอลิเมนต์พาเรนต์ |
| all | ใช้ transition effect กับทุกคุณสมบัติ |
| property | ระบุลิสต์ของคุณสมบัติ CSS ที่ต้องการ |
| none | ไม่เลือกคุณสมบัติใดๆ |
ตัวอย่างการใช้งาน
<!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%;
transition: all 1s;
}
.show {
font-size: 1.2rem;
margin: 1rem 0;
}
</style>
<body>
<h1>ตัวอย่างคุณสมบัติ transitionProperty ของ Style</h1>
<div class='circle'></div>
<button onclick="set()" class="btn">เปลี่ยน TransitionProperty</button>
<div class="show">ตอนนี้ลอง hover ที่สี่เหลี่ยมดู</div>
<script>
function set() {
document.querySelector('.circle').style.transitionProperty = "border-radius";
}
</script>
</body>
</html>
ผลลัพธ์
คลิกที่ปุ่ม "เปลี่ยน TransitionProperty" แล้วลอง hover ที่ "สี่เหลี่ยมสีแดง" เพื่อดูเอฟเฟกต์ transitionProperty

