HTML - DOM Style 物件 perspective 屬性



HTML DOM Style 物件 **perspective** 屬性指定元素距離 z=0 平面的距離,以提供元素的 3D 檢視。它應用於父元素,因為它會影響子元素。

屬性值越小,3D 效果越強,深度感越強;屬性值越大,深度感和 3D 效果越弱。

語法

設定 perspective 屬性
object.style.perspective= "length | none | initial | inherit";
獲取 perspective 屬性
object.style.perspective;

屬性值

描述
length 指定元素距檢視的距離。
none 預設值,不設定透視。
initial 用於將此屬性設定為其預設值。
inherit 用於繼承其父元素的屬性。

返回值

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

HTML DOM Style 物件 'perspective' 屬性示例

以下示例在 id 為 'perspective' 的 div 元素上設定 perspective 屬性,其效果顯示在其 id 為 'child' 的子 div 元素上。此示例還使用 **none** 值刪除 perspective 屬性。

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

支援的瀏覽器

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