CSS 立方体在变换后恢复到初始状态

CSS Cube reverts back to its initial state after transform

我不是 css 方面的专家,为了使用 css 和 html 在悬停时创建旋转立方体,我遵循了不同的教程资源。

悬停时,立方体平移并旋转。但是,当它不再活动时,我希望立方体 return 回到原来的位置。如何实现?

这是代码。

HTML

.wrap {
  -webkit-perspective: 800px;
  perspective: 800px;
  -webkit-perspective-origin: 50% 100px;
  perspective-origin: 50% 100px;
  float: left;
  margin-right: 3.5px;
  -webkit-transition: 1s ease-in-out;
  -moz-transition: 1s ease-in-out;
  -o-transition: 1s ease-in-out;
}
.cube {
  position: relative;
  width: 80px;
  -webkit-transform-style: preserve-3d;
  transform-style: preserve-3d;
  margin: 0 auto;
  margin-top: 30px;
  -webkit-animation: spin 3s infinite linear;
  animation: spin 3s infinite linear;
  -webkit-animation-play-state: paused;
  animation-play-state: paused;
}
.cube div {
  position: absolute;
  width: 50px;
  height: 50px;
}
.back {
  transform: translateZ(0px) rotateY(180deg);
  background: #057e98;
  opacity: 0.8;
}
.right {
  transform: rotateY(-270deg) translateX(0px);
  transform-origin: top right;
  background: #16a8b8;
  opacity: 0.8;
}
.left {
  transform: rotateY(270deg) translateX(0px);
  transform-origin: center left;
  background: #c25e28;
  opacity: 0.8;
}
.top {
  transform: rotateX(-90deg) translateY(-50px);
  -webkit-transform-origin: top center;
  transform-origin: top center;
  background: #c25e28;
  opacity: 0.8;
}
.bottom {
  transform: rotateX(90deg) translateY(0px);
  transform-origin: bottom center;
  background: blue;
  opacity: 0.8;
}
.front {
  transform: translateZ(50px);
  background: #f47a2d;
  opacity: 0.8;
}
@-webkit-keyframes spin {
  from {
    -webkit-transform: rotateY(0);
    -webkit-transform-origin: 20% 50% 0;
  }
  to {
    -webkit-transform: rotateY(360deg);
    -webkit-transform-origin: 20% 50% 0;
  }
}
.cube:hover {
  -webkit-animation-play-state: running;
  animation-play-state: running;
}
.wrap:hover {
  -webkit-transform: translate(3em, 5em);
}
<div class="wrap">
  <div class="cube text-center">
    <div class="front">
    </div>
    <div class="back">
    </div>
    <div class="top">
    </div>
    <!--<div class="bottom"> 
          </div>-->
    <div class="left">
    </div>
    <div class="right">
    </div>
  </div>
</div>

<div class="wrap">
  <div class="cube text-center">
    <div class="front">
    </div>
    <div class="back">
    </div>
    <div class="top">
    </div>
    <!--<div class="bottom"> 
          </div>-->
    <div class="left">
    </div>
    <div class="right">
    </div>
  </div>
</div>

问题是当立方体停止旋转时,它会保持当前状态。我希望它恢复到原来的位置和状态。也就是说,如果它停止旋转并显示 'right' 侧,则它应该 return 到其原始位置并显示 'front' 侧。

我认为您需要关注。您必须申请以下 css:

这是因为你暂停了你的动画 animation-play-state: paused; 当没有悬停时它会停止动画。

.wrap {
     -webkit-perspective: 800px;
        perspective: 800px;
        -webkit-perspective-origin: 50% 100px;
        perspective-origin: 50% 100px;
        float:left;
        margin-right: 3.5px;
        -webkit-transition: 1s ease-in-out;
        -moz-transition: 1s ease-in-out;
        -o-transition: 1s ease-in-out;    
    }

    .cube {
        position: relative;
        width: 80px;
        -webkit-transform-style: preserve-3d;
        transform-style: preserve-3d;
        margin: 0 auto;
        margin-top: 30px;        
       

    }

    .cube div {
        position: absolute;
        width: 50px;
        height: 50px;
    }

    .back {
        transform: translateZ(0px) rotateY(180deg);
        background: #057e98;
        opacity: 0.8;
    }
    .right {
        transform: rotateY(-270deg) translateX(0px);
        transform-origin: top right;
        background: #16a8b8;
        opacity: 0.8;
    }
    .left {
        transform: rotateY(270deg) translateX(0px);
        transform-origin: center left;
        background: #c25e28;
        opacity: 0.8;
    }
    .top {
        transform: rotateX(-90deg) translateY(-50px);
        -webkit-transform-origin: top center;
        transform-origin: top center;
        background: #c25e28;
        opacity: 0.8;
    }
    .bottom {
        transform: rotateX(90deg) translateY(0px);
        transform-origin: bottom center;
        background: blue;
        opacity: 0.8;
    }
    .front {
        transform: translateZ(50px);
        background: #f47a2d;
        opacity: 0.8;
    }

    @-webkit-keyframes spin {
        from { -webkit-transform: rotateY(0); -webkit-transform-origin: 20% 50% 0; }
        to { -webkit-transform: rotateY(360deg); -webkit-transform-origin: 20% 50% 0;}
    } 

@keyframes spin {
        from { transform: rotateY(0); transform-origin: 20% 50% 0; }
        to { transform: rotateY(360deg); transform-origin: 20% 50% 0;}
    } 

    .cube:hover {  
     -webkit-animation: spin 3s infinite linear;
        animation: spin 3s infinite linear;
        -webkit-animation-play-state: running;
        animation-play-state: running;
    }

    .wrap:hover{
        -webkit-transform: translate(3em,5em);
      transform: translate(3em,5em);
    }
<div class="wrap">
   <div class="cube text-center">
      <div class="front">
      </div>
      <div class="back">
      </div>
      <div class="top">
      </div>
      <!--<div class="bottom"> 
      </div>-->
      <div class="left">
      </div>
      <div class="right">
      </div>
  </div>
</div>

<div class="wrap">
   <div class="cube text-center">
      <div class="front">
      </div>
      <div class="back">
      </div>
      <div class="top">
      </div>
      <!--<div class="bottom"> 
      </div>-->
      <div class="left">
      </div>
      <div class="right">
      </div>
  </div>
</div>

希望对您有所帮助。

您可以创建一个在非 :hover 状态下出现一次的回旋动画, 但它会在页面加载时发生一次(在任何事情发生之前);

你可以让一个 js 监听器在悬停时添加这个动画来解决这个问题......(但那是作弊)..

.wrap {
  -webkit-perspective: 800px;
  perspective: 800px;
  -webkit-perspective-origin: 50% 100px;
  perspective-origin: 50% 100px;
  float: left;
  margin-right: 3.5px;
  -webkit-transition: 1s ease-in-out;
  -moz-transition: 1s ease-in-out;
  -o-transition: 1s ease-in-out;
}
.cube {
  position: relative;
  width: 80px;
  -webkit-transform-style: preserve-3d;
  transform-style: preserve-3d;
  margin: 0 auto;
  margin-top: 30px;
  -webkit-animation: spinback 3s 1 linear;
  animation: spinback 3s 1 linear;

  
}
.cube div {
  position: absolute;
  width: 50px;
  height: 50px;
}
.back {
  transform: translateZ(0px) rotateY(180deg);
  background: #057e98;
  opacity: 0.8;
}
.right {
  transform: rotateY(-270deg) translateX(0px);
  transform-origin: top right;
  background: #16a8b8;
  opacity: 0.8;
}
.left {
  transform: rotateY(270deg) translateX(0px);
  transform-origin: center left;
  background: #c25e28;
  opacity: 0.8;
}
.top {
  transform: rotateX(-90deg) translateY(-50px);
  -webkit-transform-origin: top center;
  transform-origin: top center;
  background: #c25e28;
  opacity: 0.8;
}
.bottom {
  transform: rotateX(90deg) translateY(0px);
  transform-origin: bottom center;
  background: blue;
  opacity: 0.8;
}
.front {
  transform: translateZ(50px);
  background: #f47a2d;
  opacity: 0.8;
}
@-webkit-keyframes spin {
  from {
    -webkit-transform: rotateY(0);
    -webkit-transform-origin: 20% 50% 0;
  }
  to {
    -webkit-transform: rotateY(360deg);
    -webkit-transform-origin: 20% 50% 0;
  }
}
 @-webkit-keyframes spinback {

  from {
    -webkit-transform: rotateY(360deg);
    -webkit-transform-origin: 20% 50% 0;
  }
  to {
    -webkit-transform: rotateY(0);
    -webkit-transform-origin: 20% 50% 0;
  }
}
.cube:hover {
  -webkit-animation: spin 3s infinite linear;
  animation: spin 3s infinite linear;
}
.wrap:hover {
  -webkit-transform: translate(3em, 5em);
}
<div class="wrap">
  <div class="cube text-center">
    <div class="front">
    </div>
    <div class="back">
    </div>
    <div class="top">
    </div>
    <!--<div class="bottom"> 
          </div>-->
    <div class="left">
    </div>
    <div class="right">
    </div>
  </div>
</div>

<div class="wrap">
  <div class="cube text-center">
    <div class="front">
    </div>
    <div class="back">
    </div>
    <div class="top">
    </div>
    <!--<div class="bottom"> 
          </div>-->
    <div class="left">
    </div>
    <div class="right">
    </div>
  </div>
</div>