HTML - DOM樣式物件perspectiveOrigin屬性



HTML DOM 樣式物件**perspectiveOrigin**屬性使用x軸和y軸設定3D元素的位置。它允許更改3D元素的底部位置。

透過定義perspectiveOrigin屬性,子元素的位置不是我們定義該屬性的父元素。它與perspective屬性一起使用,並且隻影響3D變換元素。

語法

設定perspectiveOrigin屬性
object.style.perspectiveOrigin= "x-axis y-axis|initial|inherit";
獲取perspectiveOrigin屬性
object.style.perspectiveOrigin;

屬性值

描述
x軸 它表示x軸上檢視的水平位置。x軸的可能值如下所示
  • 長度值
  • 百分比值
  • 居中
y軸 它表示y軸上檢視的垂直位置。y軸的可能值如下所示
  • 長度值
  • 百分比值
  • 居中
initial 用於將此屬性設定為其預設值。
inherit 用於繼承其父元素的屬性。

返回值

它返回一個字串值,表示元素的perspectiveOrigin屬性。

HTML DOM 樣式物件“perspectiveOrigin”屬性示例

下面的示例設定父div元素的perspectiveOrigin屬性。

設定perspectiveOrigin屬性

下面的示例將父div元素的perspectiveOrigin屬性設定為**左**、**右**和**居中**。

<!DOCTYPE html>
<html lang="en">
<head>
    <title>
        HTML DOM Style Object perspectiveOrigin Property
    </title>
    <style>
        #perspective {
            padding: 50px;
            height: 180px;
            width: 200px;
            border: 2px solid;
            background-color: azure;
            perspective: 100px;
        }
        #child {
            padding: 10px;
            height: 100px;
            width: 100px;
            border: 2px solid #04af2f;
            background-color: aquamarine;
            transform: rotateX(20deg);
        }
    </style>
</head>
<body>
    <p>
        Click to set Perspective Origin property.
    </p>
    <button onclick="fun()">Right</button>
    <button onclick="funTwo()">Center</button>
    <button onclick="funThree()">Left</button>
    <div id="perspective">
        <div id="child">
            Welcome to Tutorials Point...
        </div>
    </div>
    <script>
        function fun() {
            document.getElementById("perspective")
                .style.perspectiveOrigin = "right ";
        }  
        function funTwo() {
            document.getElementById("perspective")
                .style.perspectiveOrigin = "center";
        }  
        function funThree() {
            document.getElementById("perspective")
                .style.perspectiveOrigin= "left";
        }      
    </script>
</body>
</html>

使用“長度值”和“百分比值”設定perspectiveOrigin

下面的示例將父div元素的perspectiveOrigin屬性設定為**長度值**和**百分比值**。

<!DOCTYPE html>
<html lang="en">
<head>
    <title>
        HTML DOM Style Object perspectiveOrigin Property
    </title>
    <style>
        #perspective {
            padding: 50px;
            height: 180px;
            width: 200px;
            border: 2px solid;
            background-color: azure;
            perspective: 100px;
        }
        #child {
            padding: 10px;
            height: 100px;
            width: 100px;
            border: 2px solid #04af2f;
            background-color: aquamarine;
            transform: rotateX(20deg);
        }
    </style>
</head>
<body>
    <p>
        Click to set Perspective Origin property.
    </p>
    <button onclick="fun()">Using length</button>
    <button onclick="funTwo()">length & percentage</button>
    <button onclick="funThree()">Using percentage</button>
    <div id="perspective">
        <div id="child">
            Welcome to Tutorials Point...
        </div>
    </div>
    <script>
        function fun() {
            document.getElementById("perspective")
                .style.perspectiveOrigin = "5px 40px";
        }  
        function funTwo() {
            document.getElementById("perspective")
                .style.perspectiveOrigin = "40px 5%";
        }  
        function funThree() {
            document.getElementById("perspective")
                .style.perspectiveOrigin= "1% 10%";
        }      
    </script>
</body>
</html>

支援的瀏覽器

屬性 Chrome Edge Firefox Safari Opera
perspectiveOrigin 是 36 是 12 是 16 是 9 是 23
html_dom_style_object_reference.htm
廣告