当前位置: 首页>>代码示例>>用法及示例精选>>正文


HTML Style transformStyle用法及代码示例

transformStyle属性用于设置或返回嵌套元素在3D空间中进行渲染的不同方式。

用法:

  • 返回transformStyle:
    object.style.transformStyle
  • 它设置了transformStyle:
    object.style.transformStyle = "flat|preserve-3d|initial|inherit"

属性:


  • flat:它是默认属性值。但是,子元素不会保留3D位置。
  • preserve-3d:它使子元素能够保留其3D位置。
  • initial:它将transformStyle设置为其默认值。
  • inherit:它继承了父元素的transformStyle属性值。

返回值:它返回一个字符串,表示元素的transform-style属性。

示例1:显示平面属性。

<!DOCTYPE html> 
<html> 
  
<head> 
    <title> 
        HTML | DOM Style transformStyle Property 
    </title> 
    <style> 
        #DIV1 { 
            padding:0.4px; 
            position:absolute; 
            border:1px solid black; 
            background-color:green; 
            -webkit-transform:rotateY(100deg); 
            transform:rotateY(50deg); 
        } 
          
        #DIV2 { 
            padding:5px; 
            position:absolute; 
            border:1px solid black; 
            background-color:lightgreen; 
            -webkit-transform:rotateY(0deg); 
            transform:rotateY(100deg); 
        } 
    </style> 
</head> 
  
<body> 
    <h1> 
      <center> 
        Geeks  
        <button onclick="gfg()"> 
          Press 
        </button> 
      </center>  
    </h1> 
  
    <h4> 
      Clicking on the 'Press' button  
      will set the property to flat. 
    </h4> 
  
    <div id="DIV1"> 
        <h2>GeeksforGeeks</h2> 
        <div id="DIV2"> 
            <h1>12345</h1> 
        </div> 
    </div> 
  
    <script> 
        function gfg() { 
  
            //  Set transform style for Apple Safari. 
            document.getElementById( 
                "DIV1").style.WebkitTransformStyle = "flat"; 
  
            //  Set "falt" transform style. 
            document.getElementById( 
                "DIV2").style.transformStyle = "flat"; 
        } 
    </script> 
</body> 
  
</html>

输出:

  • 在单击按钮之前:
  • 单击按钮后:

示例2:显示Preserve 3D属性。

<!DOCTYPE html> 
<html> 
  
<head> 
    <title> 
        HTML | DOM Style transformStyle Property 
    </title> 
  
    <style> 
        #DIV1 { 
            padding:0.4px; 
            position:absolute; 
            border:1px solid black; 
            background-color:green; 
            -webkit-transform:rotateY(100deg); 
            transform:rotateY(50deg); 
        } 
          
        #DIV2 { 
            padding:5px; 
            position:absolute; 
            border:1px solid black; 
            background-color:lightgreen; 
            -webkit-transform:rotateY(0deg); 
            transform:rotateY(100deg); 
        } 
    </style> 
</head> 
  
<body> 
    <h1> 
       <center> 
         Geeks  
         <button onclick="gfg()"> 
           Press 
         </button> 
      </center>  
    </h1> 
  
    <h4> 
      Clicking on the 'Press' button  
      will set the property to preserve 3D. 
    </h4> 
  
    <div id="DIV1"> 
        <h2>GeeksforGeeks</h2> 
        <div id="DIV2"> 
            <h1>12345</h1> 
        </div> 
    </div> 
  
    <script> 
        function gfg() { 
  
            // Set Transform style property for Apple Safari. 
            document.getElementById( 
                    "DIV1").style.WebkitTransformStyle = 
                "preserve-3d"; 
  
            //  Set "preserve-3d" 
            document.getElementById( 
                    "DIV2").style.transformStyle = 
                "preserve-3d"; 
        } 
    </script> 
</body> 
  
</html>

输出:

  • 在单击按钮之前:

示例3:显示初始属性。

<!DOCTYPE html> 
<html> 
  
<head> 
    <title> 
        HTML | DOM Style transformStyle Property 
    </title> 
    <style> 
        #DIV1 { 
            padding:0.4px; 
            position:absolute; 
            border:1px solid black; 
            background-color:green; 
            -webkit-transform:rotateY(100deg); 
            transform:rotateY(50deg); 
        } 
          
        #DIV2 { 
            padding:5px; 
            position:absolute; 
            border:1px solid black; 
            background-color:lightgreen; 
            -webkit-transform:rotateY(0deg); 
            transform:rotateY(100deg); 
        } 
    </style> 
  
    <body> 
        <h1> 
           <center> 
             Geeks  
             <button onclick="gfg()"> 
               Press 
             </button> 
           </center>  
         </h1> 
  
         <h4> 
          Clicking on the 'Press' button  
          will set the property to initial. 
         </h4> 
  
         <div id="DIV1"> 
            <h2>GeeksforGeeks</h2> 
            <div id="DIV2"> 
                <h1>12345</h1> 
            </div> 
         </div> 
  
        <script> 
            function gfg() { 
  
                //  Set Transform style property for Apple Safari 
                document.getElementById( 
                        "DIV1").style.WebkitTransformStyle = 
                    "initial"; 
  
                //  Set "initial" Transform style 
                document.getElementById( 
                        "DIV2").style.transformStyle = 
                    "initial"; 
            } 
        </script> 
  
    </body> 
  
</html>

输出:

  • 在单击按钮之前:
  • 单击按钮后:

示例4:显示继承属性。

<!DOCTYPE html> 
<html> 
  
<head> 
    <title> 
        HTML | DOM Style transformStyle Property 
    </title> 
    <style> 
        #DIV1 { 
            padding:0.4px; 
            position:absolute; 
            border:1px solid black; 
            background-color:green; 
            -webkit-transform:rotateY(100deg); 
            transform:rotateY(50deg); 
        } 
          
        #DIV2 { 
            padding:5px; 
            position:absolute; 
            border:1px solid black; 
            background-color:lightgreen; 
            -webkit-transform:rotateY(0deg); 
            transform:rotateY(100deg); 
        } 
    </style> 
  
</head> 
  
<body> 
    <h1> 
      <center>Geeks  
        <button onclick="gfg()"> 
          Press 
        </button> 
      </center> 
    </h1> 
  
    <h4> 
      Clicking on the 'Press' button  
      will set the property to inherit. 
    </h4> 
  
    <div id="DIV1"> 
        <h2>GeeksforGeeks</h2> 
        <div id="DIV2"> 
            <h1>12345</h1></div> 
    </div> 
  
    <script> 
        function gfg() { 
  
            // Set Transform property for Apple Safari. 
            document.getElementById( 
                    "DIV1").style.WebkitTransformStyle = 
                "inherit"; 
  
            //  Set "inherit" transform property. 
            document.getElementById( 
                    "DIV2").style.transformStyle = 
                "inherit"; 
        } 
    </script> 
</body> 
  
</html>

输出:

  • 在单击按钮之前:
  • 单击按钮后:

注意:Apple Safari不支持此属性。

浏览器支持:DOM样式transformStyle属性支持的浏览器如下:

  • 谷歌浏览器
  • IE浏览器
  • Firefox
  • Opera


相关用法

注:本文由纯净天空筛选整理自riarawal99大神的英文原创作品 HTML | DOM Style transformStyle Property。非经特殊声明,原始代码版权归原作者所有,本译文的传播和使用请遵循“署名-相同方式共享 4.0 国际 (CC BY-SA 4.0)”协议。