围绕Y轴旋转而不改变位置

时间:2017-12-28 15:08:26

标签: css html5 animation 3d transform

我正在尝试构建一个金字塔并为其设置动画。问题是在围绕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>

1 个答案:

答案 0 :(得分:0)

只需从 .base 中删除position:absolute,然后从 .pyramid 删除width:0,然后添加display:inline-block

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

&#13;
&#13;
.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);
  }
}
&#13;
<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>
&#13;
&#13;
&#13;