HTML DOM Style transformStyle 属性
htmlweb developmentfront end technology更新于 2025/6/11 2:20:17
HTML DOM Style transformStyle 属性返回并应用 2D 或 3D 变换到 HTML 文档中的元素。
语法
以下是语法 −
返回 transformStyle
object.style.transformStyle
修改 transformStyle
object.style.transformStyle = “value”
值
此处,值可以为 -
| 值 | 说明 |
|---|---|
| inherit | 从其父元素继承此属性值。 |
| initial | 将此属性值设置为其默认值。 |
| flat | 设置子元素不保留其 3D 位置。 |
| preserve-3d | 设置子元素保留其 3D 位置。 |
示例
让我们看一个 HTML DOM Style transformStyle 属性的示例 −
<!DOCTYPE html>
<html>
<head>
<style>
body {
background: lightgreen;
height: 100vh;
text-align: center;
}
.outer-box {
position: relative;
height: 200px;
width: 200px;
margin: 80px;
padding: 5px;
border: 2px solid black;
}
.inner-box1 {
padding: 50px;
position: absolute;
background-color: coral;
transform: rotateY(40deg);
}
.inner-box2 {
padding: 40px;
position: absolute;
background-color: lightblue;
transform: rotateY(60deg);
}
.btn {
background: #db133a;
border: none;
height: 2rem;
border-radius: 2px;
width: 40%;
display: block;
color: #fff;
outline: none;
cursor: pointer;
margin: 1rem auto;
}
</style>
</head>
<body>
<h1>DOM Style transformStyle Property Example</h1>
<div class="outer-box">Outer Box
<div class="inner-box1">Inner Box1
<div class="inner-box2">Inner Box2</div>
</div>
</div>
<button onclick="add()" class="btn">Set transformStyle</button>
<script>
function add() {
document.querySelector(".inner-box1").style.transformStyle = "preserve-3d";
}
</script>
</body>
</html>
输出
这将产生以下输出 −

点击"设置 transformStyle"按钮,将变换样式应用于 Inner Box 1 −

相关文章
有用资源
html 参考教程 - 该教程包含有关 html 的更多信息:https://www.cainiaomax.com/html/

