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

คุณสมบัติ perspective ของ Style ใน HTML DOM

คุณสมบัติ perspective ของ HTML DOM Style ใช้สำหรับคืนค่าและแก้ไขวิธีที่องค์ประกอบ 3D ถูกมองเห็นในเอกสาร HTML

ไวยากรณ์

1. การคืนค่า perspective

object.perspective

2. การแก้ไข perspective

object.perspective = "value"

ที่นี่ value สามารถเป็นได้ดังนี้:

ค่าคำอธิบาย
Initialตั้งค่าคุณสมบัตินี้เป็นค่าเริ่มต้น
inheritสืบทอดค่าคุณสมบัติจากองค์ประกอบพาเรนต์
Noneไม่ตั้งค่ามุมมอง
lengthระบุระยะห่างขององค์ประกอบจากมุมมองในหน่วยความยาว

ตัวอย่าง

<!DOCTYPE html>
<html>
<style>
  body {
    color: #000;
    height: 100vh;
    background-color: #8BC6EC;
    background-image: linear-gradient(135deg, #8BC6EC 0%, #9599E2 100%);
  }
  .btn {
    background: #db133a;
    border: none;
    height: 2rem;
    border-radius: 2px;
    width: 40%;
    display: block;
    color: #fff;
    outline: none;
    cursor: pointer;
    margin: 1rem auto;
  }
  .box {
    text-align: center;
    width: 200px;
    height: 200px;
    background-color: #db133a;
    transform: rotateX(25deg);
    margin: 2rem auto;
  }
</style>
<body>
<h1 style="text-align:center">ตัวอย่างคุณสมบัติ Style perspective ของ DOM</h1>
<div class='box'></div>
<button class="btn" onclick="set()">ตั้งค่า Perspective</button>
<script>
  function set() {
    document.body.style.perspective = "100px";
  }
</script>
</body>
</html>

ผลลัพธ์

คลิกปุ่ม "ตั้งค่า Perspective" เพื่อตั้งค่ามุมมองของกล่อง สีแดง