rotate3d() CSS 函数定义一个变换,它将元素围绕固定轴移动而不使其变形。运动量由指定的角度定义; 如果为正,运动将为顺时针,如果为负,则为逆时针。
CSS Demo: rotate3d()
transform: rotate3d(0);
transform: rotate3d(1, 1, 1, 45deg);
transform: rotate3d(2, -1, -1, -0.2turn);
transform: rotate3d(0, 1, 0.5, 3.142rad);
<section class="default-example" id="default-example">
<div class="transition-all" id="example-element">
<div class="face front">1</div>
<div class="face back">2</div>
<div class="face right">3</div>
<div class="face left">4</div>
<div class="face top">5</div>
<div class="face bottom">6</div>
</div>
</section>
#default-example {
background: linear-gradient(skyblue, khaki);
perspective: 550px;
}
#example-element {
width: 100px;
height: 100px;
transform-style: preserve-3d;
}
.face {
display: flex;
align-items: center;
justify-content: center;
width: 100%;
height: 100%;
position: absolute;
backface-visibility: inherit;
font-size: 60px;
color: white;
}
.front {
background: rgba(90, 90, 90, 0.7);
transform: translateZ(50px);
}
.back {
background: rgba(0, 210, 0, 0.7);
transform: rotateY(180deg) translateZ(50px);
}
.right {
background: rgba(210, 0, 0, 0.7);
transform: rotateY(90deg) translateZ(50px);
}
.left {
background: rgba(0, 0, 210, 0.7);
transform: rotateY(-90deg) translateZ(50px);
}
.top {
background: rgba(210, 210, 0, 0.7);
transform: rotateX(90deg) translateZ(50px);
}
.bottom {
background: rgba(210, 0, 210, 0.7);
transform: rotateX(-90deg) translateZ(50px);
}
在 3D 空间之中,旋转有 3 个自由维度,描述了旋转轴。旋转轴由一组 [x, y, z] 矢量定义,并且通过变换源点传递(即通过 transform-origin CSS 属性定义)。如果这些矢量被赋予非标准值,即 3 个坐标值的平方和不等于 1 时,它将会被内部隐式标准化。非标准矢量,例如空值和 [0, 0, 0],将会使旋转不起作用,但是不影响整个 CSS 属性的其他效果(译者注:如 transform 中的多项变换)。
[!NOTE] 与平面旋转相反的是,3D 旋转的组合通常是不可交换的;这意味着定义旋转规则的值的顺序是严格控制的。
语法
rotate3d(x, y, z, a)
值
- x
- : <number> 类型,可以是 0 到 1 之间的数值,表示旋转轴 X 坐标方向的矢量。
- y
- : <number> 类型,可以是 0 到 1 之间的数值,表示旋转轴 Y 坐标方向的矢量。
- z
- : <number> 类型,可以是 0 到 1 之间的数值,表示旋转轴 Z 坐标方向的矢量。
- a
- : <angle> 类型,表示旋转角度。正的角度值表示顺时针旋转,负值表示逆时针旋转。
<table class="standard-table"> <tbody> <tr> <th scope="col">在ℝ<sup>2</sup>上的笛卡尔坐标</th> <td rowspan="2">这种变换应用于 3D 空间,不可用于平面空间</td> </tr> <tr> <th scope="col">在ℝℙ<sup>2</sup>上的齐次坐标</th> </tr> <tr> <th scope="col">在ℝ<sup>3</sup>上的笛卡尔坐标</th> <td colspan="1"> <a href="/zh-CN/docs/Web/CSS/transform-function/rotate3d/transform-functions-rotate3d_cart.png" ><img src="transform-functions-rotate3d_cart.png" style="height: 47px; width: 510px" /></a ><math> <mfenced ><mtable ><mtr ><mtd >1<mo>+</mo>(1<mo>-</mo>cos(<mi>a</mi>))(<msup ><mi>x</mi><mn>2</mn></msup ><mo>-</mo>1)</mtd ><mtd ><mi>z</mi><mo>·</mo>sin(<mi>a</mi>)+<mi>x</mi ><mi>y</mi>(1<mo>-</mo>cos(<mi>a</mi>))</mtd ><mtd ><mo>-</mo><mi>y</mi><mo>·</mo>sin(<mi>a</mi>)<mo>+</mo ><mi>x</mi><mi>z</mi ><mo>·</mo>(1<mo>-</mo>cos(<mi>a</mi>))</mtd ></mtr ><mtr ><mtd ><mo>-</mo><mi>z</mi><mo>·</mo>sin(<mi>a</mi>)<mo>+</mo ><mi>x</mi><mi>y</mi ><mo>·</mo>(1<mo>-</mo>cos(<mi>a</mi>))</mtd ><mtd>1+(1-cos(a))(y2-1)</mtd ><mtd ><mi>x</mi><mo>·</mo>sin(<mi>a</mi>)<mo>+</mo><mi>y</mi ><mi>z</mi><mo>·</mo>(1<mo>-</mo>cos(<mi>a</mi>))</mtd ><mtr ><mtd>ysin(a) + xz(1-cos(a))</mtd ><mtd>-xsin(a)+yz(1-cos(a))</mtd><mtd>1+(1-cos(a))(z2-1)</mtd ><mtd>t</mtd></mtr ><mtr ><mtd>0</mtd><mtd>0</mtd><mtd>0</mtd><mtd>1</mtd></mtr ></mtr ></mtable ></mfenced ></math > </td> </tr> <tr> <th scope="col">在ℝℙ<sup>3</sup>上的齐次坐标</th> <td colspan="1"> <a href="/zh-CN/docs/Web/CSS/transform-function/rotate3d/transform-functions-rotate3d_hom4.png" ><img src="transform-functions-rotate3d_hom4.png" style="height: 61px; width: 522px" /></a> </td> </tr> </tbody> </table>
示例
绕 Y 轴旋转
HTML
<div>Normal</div>
<div class="rotated">Rotated</div>
CSS
body {
perspective: 800px;
}
div {
width: 80px;
height: 80px;
background-color: skyblue;
}
.rotated {
transform: rotate3d(0, 1, 0, 60deg);
background-color: pink;
}
效果
绕 Y 轴旋转
围绕自定义轴旋转
HTML
<div>Normal</div>
<div class="rotated">Rotated</div>
CSS
body {
perspective: 800px;
}
div {
width: 80px;
height: 80px;
background-color: skyblue;
}
.rotated {
transform: rotate3d(1, 2, -1, 192deg);
background-color: pink;
}
效果
围绕自定义轴旋转
规范
浏览器兼容性
参见
- transform
- <transform-function>
来源:MDN Web Docs / Mozilla 贡献者 · 作者与贡献记录 · CC BY-SA 2.5。离线整理了版式、链接和媒体显示。
