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

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了围绕Y轴旋转而不改变位置相关的知识,希望对你有一定的参考价值。

我正在尝试建立一个金字塔并为其制作动画。问题是在围绕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>
答案

只需从.pyramid中删除.base和position:absolute中的width:0并添加display:inline-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>

以上是关于围绕Y轴旋转而不改变位置的主要内容,如果未能解决你的问题,请参考以下文章

通过金属中的任意 (x,y) 点围绕 z 轴旋转

变形--扭曲 skew()

three.js 在它的中心围绕 Y 轴旋转 Object3d

如何围绕节点的 Y 轴旋转 SKSpriteNode?

围绕 y 轴旋转两个图像,使它们的边界相交

ARcore:如何围绕 Y 轴旋转对象?