CSS立方体中的RotateX无法正常工作

时间:2016-05-31 09:22:57

标签: html css css3

我正在学习创建立方体旋转效果。在悬停时如果我将rotateX替换为rotateY,则立方体围绕Y轴旋转居中。但是当rotateX存在时,立方体不会围绕X轴居中旋转。如何实现立方体的正确旋转?



#container {
  perspective: 1000px;
  perspective-origin: 0 0;
}
#cube {
  position: relative;
  top: 100px;
  left: 100px;
  width: 200px;
  transform-style: preserve-3d;
  transition: transform 2s;
  transform-origin: 50% 50%;
}
#cube div {
  position: absolute;
  width: 200px;
  height: 200px;
}
#front {
  transform: rotateY(   0deg ) translateZ( 100px );
  background-color: rgba(0,34,62,0.3);
}
#right {
  transform: rotateY(  90deg ) translateZ( 100px );
  background-color: rgba(110,34,162,0.3);
}
#back {
  transform: rotateY( 180deg ) translateZ( 100px );
  background-color: rgba(20,4,62,0.3);
}
#left {
  transform: rotateY( -90deg ) translateZ( 100px );
  background-color: rgba(80,134,2,0.3);
}
#top {
  transform: rotateX(90deg) translateZ(100px);
}
#bottom {
  transform: rotateX(-90deg) translateZ(100px);
}
#cube:hover {
  transform: rotateX(360deg);
}

<html>
  <body>
    <div id="container">
      <div id="cube">
        <div id="front">
          <h1>1</h1>
        </div>
        <div id="right">
          <h1>2</h1>
        </div>
        <div id="back">
          <h1>3</h1>
        </div>
        <div id="left">
          <h1>4</h1>
        </div>
        <div id="top">
          <h1>5</h1>
        </div>
        <div id="bottom">
          <h1>6</h1>
        </div>
      </div>
    </div>
  </body>
</html>
&#13;
&#13;
&#13;

2 个答案:

答案 0 :(得分:2)

如果我理解正确,您只需将#cube的身高设置为200px

#container {
  perspective: 1000px;
  perspective-origin: 0 0;
}
#cube {
  position: relative;
  top: 100px;
  left: 100px;
  width: 200px;
  height:200px;
  transform-style: preserve-3d;
  transition: transform 2s;
  transform-origin: 50% 50%;
}
#cube div {
  position: absolute;
  width: 200px;
  height: 200px;
}
#front {
  transform: rotateY(   0deg ) translateZ( 100px );
  background-color: rgba(0,34,62,0.3);
}
#right {
  transform: rotateY(  90deg ) translateZ( 100px );
  background-color: rgba(110,34,162,0.3);
}
#back {
  transform: rotateY( 180deg ) translateZ( 100px );
  background-color: rgba(20,4,62,0.3);
}
#left {
  transform: rotateY( -90deg ) translateZ( 100px );
  background-color: rgba(80,134,2,0.3);
}
#top {
  transform: rotateX(90deg) translateZ(100px);
}
#bottom {
  transform: rotateX(-90deg) translateZ(100px);
}
#cube:hover {
  transform: rotateX(360deg);
}
<html>
  <body>
    <div id="container">
      <div id="cube">
        <div id="front">
          <h1>1</h1>
        </div>
        <div id="right">
          <h1>2</h1>
        </div>
        <div id="back">
          <h1>3</h1>
        </div>
        <div id="left">
          <h1>4</h1>
        </div>
        <div id="top">
          <h1>5</h1>
        </div>
        <div id="bottom">
          <h1>6</h1>
        </div>
      </div>
    </div>
  </body>
</html>

答案 1 :(得分:1)

您需要根据div大小(cude的一侧)设置变换原点。所以我只是更改了多维数据集的transform-origin: 100px 100px;

&#13;
&#13;
#container {
  perspective: 1000px;
  perspective-origin: 0 0;
  height: 500px;
}
#cube {
  position: relative;
  top: 100px;
  left: 100px;
  width: 200px;
  transform-style: preserve-3d;
  transition: transform 2s;
  transform-origin: 100px 100px;
}
#cube div {
  position: absolute;
  width: 200px;
  height: 200px;
}
#front {
  transform: rotateY(   0deg ) translateZ( 100px );
  background-color: rgba(0,34,62,0.3);
}
#right {
  transform: rotateY(  90deg ) translateZ( 100px );
  background-color: rgba(110,34,162,0.3);
}
#back {
  transform: rotateY( 180deg ) translateZ( 100px );
  background-color: rgba(20,4,62,0.3);
}
#left {
  transform: rotateY( -90deg ) translateZ( 100px );
  background-color: rgba(80,134,2,0.3);
}
#top {
  transform: rotateX(90deg) translateZ(100px);
}
#bottom {
  transform: rotateX(-90deg) translateZ(100px);
}
#cube:hover {
  transform: rotateX(360deg);
}
&#13;
<html>
  <body>
    <div id="container">
      <div id="cube">
        <div id="front">
          <h1>1</h1>
        </div>
        <div id="right">
          <h1>2</h1>
        </div>
        <div id="back">
          <h1>3</h1>
        </div>
        <div id="left">
          <h1>4</h1>
        </div>
        <div id="top">
          <h1>5</h1>
        </div>
        <div id="bottom">
          <h1>6</h1>
        </div>
      </div>
    </div>
  </body>
</html>
&#13;
&#13;
&#13;

由于立方体不是&#34;直的&#34;它不适用于百分比。双面,容器使用透视。