gpt4 book ai didi

css - 绕 Y 轴旋转而不改变位置

转载 作者:行者123 更新时间:2023-11-28 15:12:32 29 4
gpt4 key购买 nike

我正在尝试构建金字塔并为其设置动画。问题是在围绕 X 轴旋转它之后,我尝试围绕金字塔的高度(Y 轴)旋转它,但我每次都在移动。我基本上尝试了每个 transform-origin 选项,但它不起作用。我的代码如下:

感谢帮助

.cube{
margin: 100px 200px;
}

.pyramid{
width: 0;
position: relative;
transform-style: preserve-3d;
animation: spin-pyramid 20s ease infinite alternate;
}

.base{
width: 200px;
height: 200px;
background: rgba(255,0,0,.5);
position: absolute;
}

.side{
width: 0;
height: 0;
border-bottom: 141.421px solid rgba(255, 255, 0, 0.5);
border-left: 100px solid transparent;
border-right: 100px solid transparent;
position: absolute;
top: 100px;
left: 0;
transform-origin: 50% 0;
}

.side-1{
transform: translateZ(100px) rotateX(-45deg);
}

.side-2{
border-bottom-color: rgba(0, 255, 255, 0.5);
transform: rotateZ(90deg) translateZ(100px) rotateX(-45deg);
}

.side-3{
border-bottom-color: rgba(255, 0, 255, 0.5);
transform: rotateZ(180deg) translateZ(100px) rotateX(-45deg);
}

.side-4{
border-bottom-color: rgba(0, 255, 0, 0.5);
transform: rotateZ(270deg) translateZ(100px) rotateX(-45deg);
}

@keyframes spin-pyramid {
10%{transform: rotateX(90deg);}
25%{transform: rotateY(90deg) rotateX(90deg);}
50%{transform: rotateY(180deg) rotateX(90deg);}
75%{transform: rotateY(270deg) rotateX(90deg);}
100%{transform: rotateY(360deg) rotateX(90deg);}
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<div class="cube">
<div class="pyramids">
<div class="pyramid pyramid-1">
<div class="base"></div>
<div class="side side-1"></div>
<div class="side side-2">1.2</div>
<div class="side side-3">1.3</div>
<div class="side side-4">1.4</div>
</div>
<div class="pyramid pyramid-2"></div>
<div class="pyramid pyramid-3"></div>
<div class="pyramid pyramid-4"></div>
<div class="pyramid pyramid-5"></div>
<div class="pyramid pyramid-6"></div>
</div>
</div>
</body>
</html>

最佳答案

只需从 .base 中删除 position:absolute 并从 .pyramid 中删除 width:0 并添加显示:内联 block

这将为金字塔元素(您想要旋转)提供底部的高度和宽度,因此旋转将相对于底部执行。

.cube {
margin: 100px 200px;
}

.pyramid {
position: relative;
display:inline-block;
transform-style: preserve-3d;
animation: spin-pyramid 20s ease infinite alternate;
}

.base {
width: 200px;
height: 200px;
background: rgba(255, 0, 0, .5);
}

.side {
height:0;
width:0;
border-bottom: 141.421px solid rgba(255, 255, 0, 0.5);
border-left: 100px solid transparent;
border-right: 100px solid transparent;
position: absolute;
top: 100px;
left: 0;
transform-origin: 50% 0;
}

.side-1 {
transform: translateZ(100px) rotateX(-45deg);
}

.side-2 {
border-bottom-color: rgba(0, 255, 255, 0.5);
transform: rotateZ(90deg) translateZ(100px) rotateX(-45deg);
}

.side-3 {
border-bottom-color: rgba(255, 0, 255, 0.5);
transform: rotateZ(180deg) translateZ(100px) rotateX(-45deg);
}

.side-4 {
border-bottom-color: rgba(0, 255, 0, 0.5);
transform: rotateZ(270deg) translateZ(100px) rotateX(-45deg);
}

@keyframes spin-pyramid {
10% {
transform: rotateX(90deg);
}
25% {
transform: rotateY(90deg) rotateX(90deg);
}
50% {
transform: rotateY(180deg) rotateX(90deg);
}
75% {
transform: rotateY(270deg) rotateX(90deg);
}
100% {
transform: rotateY(360deg) rotateX(90deg);
}
}
<div class="cube">
<div class="pyramids">
<div class="pyramid pyramid-1">
<div class="base"></div>
<div class="side side-1"></div>
<div class="side side-2">1.2</div>
<div class="side side-3">1.3</div>
<div class="side side-4">1.4</div>
</div>
<div class="pyramid pyramid-2"></div>
<div class="pyramid pyramid-3"></div>
<div class="pyramid pyramid-4"></div>
<div class="pyramid pyramid-5"></div>
<div class="pyramid pyramid-6"></div>
</div>
</div>

关于css - 绕 Y 轴旋转而不改变位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48009813/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com