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

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

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

ไวยากรณ์

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

object.perspectiveOrigin

2. การแก้ไข perspectiveOrigin

object.perspectiveOrigin = "value"

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

ค่าคำอธิบาย
initialตั้งค่าคุณสมบัตินี้เป็นค่าเริ่มต้น
inheritสืบทอดค่าคุณสมบัติจากองค์ประกอบพาเรนต์
x-axis y-axisx-axis แทนตำแหน่งมุมมองบนแกน x, y-axis แทนตำแหน่งมุมมองบนแกน y

ตัวอย่าง

<!DOCTYPE html>
<html>
<style>
  body {
    color: #000;
    height: 100vh;
    background-color: #8BC6EC;
    background-image: linear-gradient(135deg, #8BC6EC 0%, #9599E2 100%);
    perspective: 100px;
  }
  .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 perspectiveOrigin ของ DOM</h1>
<div class='box'></div>
<button class="btn" onclick="set()">ตั้งค่า PerspectiveOrigin</button>
<script>
  function set() {
    document.body.style.perspectiveOrigin = "20% 50%";
  }
</script>
</body>
</html>

ผลลัพธ์

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