HTML DOM Style transformOrigin 属性
htmlweb developmentfront end technology更新于 2025/6/11 2:52:17
HTML DOM Style transformOrigin 属性返回 HTML 文档中元素的 2D 或 3D 变换,并应用该变换。
语法
以下是语法 −
返回 transformOrigin
object.style.transformOrigin
修改 transformOrigin
object.style.transformOrigin = “value”
值
此处,值可以是负号
| 值 | 说明 |
|---|---|
| inherit | 从其父元素继承此属性值。 |
| initial | 将此属性值设置为其默认值。 |
| x 轴 y 轴 z 轴 | 分别设置视图在 x 轴、y 轴和 z 轴上的位置。 |
示例
让我们看一个 HTML DOM Style transformOrigin 属性的示例 −
<!DOCTYPE html>
<html>
<head>
<style>
body {
color: #000;
background: lightblue;
height: 100vh;
text-align: center;
}
.box {
background: lightcoral;
width: 200px;
height: 200px;
margin: 2rem auto;
}
.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 transformOrigin Property Example</h1>
<div class="box">
</div>
<button onclick="add()" class="btn">Change transformOrigin</button>
<script>
function add() {
document.querySelector('.box').style.transform = "translate(10px,10px) rotate(10deg)";
document.querySelector('.box').style.transformOrigin = "0 0";
}
</script>
</body>
</html>
输出
这将产生以下输出 −

点击“更改 transformOrigin” 按钮可以更改红色框的变换原点。

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

