问题
I was reading about the CSS rotate3d() property to rotate an element. I thought that the first three parameters in the function are multipliers and the fourth one is the degree magnitude.
The value of multipliers has to be between 0 and 1. This means that rotate3d(1, 0, 0, 90deg)
will only result in rotateX(90deg). To get more range of rotations I changed it to rotate3d(0.25, 0, 0, 360deg)
but it produces entirely different result.
I also noticed that transform: rotateX(45deg) rotateY(45deg);
is not the same as transform: rotate3d(1,1,0, 45deg);
. etc. If we cannot simply change the multiplier of required dimensions to get desired rotations, how should I calculate the value of three multipliers?
Thanks.
回答1:
When using rotate3d(x, y, z, a)
the first 3 numbers are coordinate that will define the vector of the rotation and a
is the angle of rotation. They are not multiplier of the rotation.
rotate3d(1, 0, 0, 90deg)
is the same as rotate3d(0.25, 0, 0, 90deg)
and also the same as rotate3d(X, 0, 0, 90deg)
because we will have the same vector in all the cases. Which is also the same as rotateX(90deg)
.box {
margin:30px;
padding:20px;
background:red;
display:inline-block;
}
<div class="box" style="transform:rotate3d(1,0,0,60deg)"></div>
<div class="box" style="transform:rotate3d(99,0,0,60deg)"></div>
<div class="box" style="transform:rotate3d(0.25,0,0,60deg)"></div>
<div class="box" style="transform:rotate3d(100,0,0,60deg)"></div>
<div class="box" style="transform:rotate3d(-5,0,0,60deg)"></div>
<div class="box" style="transform:rotateX(60deg)"></div>
From this we can also conclude that rotate3d(0, Y, 0, a)
is the same as rotateY(a)
and rotate3d(0, 0, Y, a)
the same as rotate(a)
. Note the use of 0
in two of the coordinates which will make our vector always in the same axis (X or Y or Z)
rotate3d(1,1,0, 45deg)
is not the same as rotateX(45deg) rotateY(45deg)
. The first one will perform one rotation around the vector defined by (1,1,0)
and the second one will perform two consecutive rotation around the X and Y axis.
In other words, rotate3d()
is not a combination of the other rotation but a rotation on its own. The other rotation are particular cases of rotate3d()
considering predefined axis.
The multiplier trick apply to the coordinate if you keep the same angle. rotate3d(x, y, z, a)
is equivalent to rotate3d(p*x, p*y, p*z, a)
because if you multiply all the coordinates with the same value, you keep the same vector direction and you change only the vector dimension which is irrelevant when defining the rotation. Only the direction is relevant.
More details here: https://developer.mozilla.org/en-US/docs/Web/CSS/transform-function/rotate3d
You can clearly notice that using values in the range of [-1,1]
for x,y,z
is enough to define all the combination. In the other hand, any combination of x,y,z
can be reduced to values inside the range [-1,1]
Examples:
.box {
margin:30px;
padding:20px;
background:red;
display:inline-block;
}
<div class="box" style="transform:rotate3d(10,5,-9,60deg)"></div>
<div class="box" style="transform:rotate3d(1,0.5,-0.9,60deg)"></div>
<div class="box" style="transform:rotate3d(25,-5,-8,60deg)"></div>
<div class="box" style="transform:rotate3d(1,-0.2,-0.32,60deg)"></div>
We simply divide by the biggest number.
来源:https://stackoverflow.com/questions/60465621/css-rotate3d-property-behaving-unexpectedly