在 3D space 中旋转子 div 垂直于父 div
Rotate child div perpendicular to parent div in 3D space
html {
font-size: 0.7rem;
}
div div {
top: 5rem;
background-color: #444;
transform-origin: top center;
}
.rotate_x {
animation-name: rotate_x;
}
@keyframes rotate_x {
0% {
transform: rotateX( 0deg );
}
100% {
transform: rotateX( 360deg );
}
}
.rotate_y {
animation-name: rotate_y;
}
@keyframes rotate_y {
0% {
transform: rotateY( 0deg );
}
100% {
transform: rotateY( 180deg );
}
}
<style>
* {
box-sizing: border-box;
margin: 0;
padding: 0;
animation-duration: 5s;
animation-iteration-count: infinite;
animation-timing-function: linear;
}
html, body {
display: flex;
justify-content: center;
align-items: center;
height: 100%;
width: 100%;
}
body {
perspective: 5rem;
}
div {
position: relative;
top: -1.5rem;
width: 5rem;
height: 5rem;
background-color: #222;
border-radius: 0.5rem;
}
</style>
<div class='rotate_y'>
<div class='rotate_x'>
</div>
</div>
灰色底部 div
似乎没有按预期旋转垂直于黑色顶部 div
。相反,底部灰色 div 似乎会缩小,直到它具有负值并反转。期望的结果是底部灰色 div
像在铰链上一样向上旋转;在 3D 中制作 'L' 形状 space
在它一路上升并翻转以在另一侧做同样的事情之前。当父 div 旋转 360 度时。
如何让底部 div
创建一个 'L' 形状,因为它与它的父级 div 相连?
需要设置transform-style
,视角再大一点:
html {
font-size: 0.7rem;
}
div div {
top: 5rem;
background-color: #444;
transform-origin: top center;
}
.rotate_x {
animation-name: rotate_x;
}
@keyframes rotate_x {
0% {
transform: rotateX( 0deg );
}
100% {
transform: rotateX( 360deg );
}
}
.rotate_y {
animation-name: rotate_y;
transform-style:preserve-3d; /* HERE */
}
@keyframes rotate_y {
0% {
transform: rotateY( 0deg );
}
100% {
transform: rotateY( 180deg );
}
}
<style>
* {
box-sizing: border-box;
margin: 0;
padding: 0;
animation-duration: 5s;
animation-iteration-count: infinite;
animation-timing-function: linear;
}
html, body {
display: flex;
justify-content: center;
align-items: center;
height: 100%;
width: 100%;
}
body {
perspective: 15rem;
}
div {
position: relative;
top: -1.5rem;
width: 5rem;
height: 5rem;
background-color: #222;
border-radius: 0.5rem;
}
</style>
<div class='rotate_y'>
<div class='rotate_x'>
</div>
</div>
html {
font-size: 0.7rem;
}
div div {
top: 5rem;
background-color: #444;
transform-origin: top center;
}
.rotate_x {
animation-name: rotate_x;
}
@keyframes rotate_x {
0% {
transform: rotateX( 0deg );
}
100% {
transform: rotateX( 360deg );
}
}
.rotate_y {
animation-name: rotate_y;
}
@keyframes rotate_y {
0% {
transform: rotateY( 0deg );
}
100% {
transform: rotateY( 180deg );
}
}
<style>
* {
box-sizing: border-box;
margin: 0;
padding: 0;
animation-duration: 5s;
animation-iteration-count: infinite;
animation-timing-function: linear;
}
html, body {
display: flex;
justify-content: center;
align-items: center;
height: 100%;
width: 100%;
}
body {
perspective: 5rem;
}
div {
position: relative;
top: -1.5rem;
width: 5rem;
height: 5rem;
background-color: #222;
border-radius: 0.5rem;
}
</style>
<div class='rotate_y'>
<div class='rotate_x'>
</div>
</div>
灰色底部 div
似乎没有按预期旋转垂直于黑色顶部 div
。相反,底部灰色 div 似乎会缩小,直到它具有负值并反转。期望的结果是底部灰色 div
像在铰链上一样向上旋转;在 3D 中制作 'L' 形状 space
在它一路上升并翻转以在另一侧做同样的事情之前。当父 div 旋转 360 度时。
如何让底部 div
创建一个 'L' 形状,因为它与它的父级 div 相连?
需要设置transform-style
,视角再大一点:
html {
font-size: 0.7rem;
}
div div {
top: 5rem;
background-color: #444;
transform-origin: top center;
}
.rotate_x {
animation-name: rotate_x;
}
@keyframes rotate_x {
0% {
transform: rotateX( 0deg );
}
100% {
transform: rotateX( 360deg );
}
}
.rotate_y {
animation-name: rotate_y;
transform-style:preserve-3d; /* HERE */
}
@keyframes rotate_y {
0% {
transform: rotateY( 0deg );
}
100% {
transform: rotateY( 180deg );
}
}
<style>
* {
box-sizing: border-box;
margin: 0;
padding: 0;
animation-duration: 5s;
animation-iteration-count: infinite;
animation-timing-function: linear;
}
html, body {
display: flex;
justify-content: center;
align-items: center;
height: 100%;
width: 100%;
}
body {
perspective: 15rem;
}
div {
position: relative;
top: -1.5rem;
width: 5rem;
height: 5rem;
background-color: #222;
border-radius: 0.5rem;
}
</style>
<div class='rotate_y'>
<div class='rotate_x'>
</div>
</div>