我不是css的专家,我已经遵循不同的教程来源,以便在使用css和html悬停时创建旋转立方体。
悬停时,立方体平移并旋转。但是,当它不再活动时,我希望立方体返回到原始位置。如何实现这一目标?
这是代码。
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>
问题是当立方体停止旋转时,它会保持当前状态。我希望它恢复到原来的位置和状态。也就是说,如果它在“右”侧显示时停止旋转,它应该返回其原始位置,并显示“前”侧。
答案 0 :(得分:2)
我认为你需要跟随。你必须申请以下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>
希望它有所帮助。
答案 1 :(得分:0)
你可以创建一个在非:悬停状态下出现一次的回旋动画, 但它会在页面加载时发生一次(在任何事情发生之前);
你可以让一个js listner在悬停时添加这个动画来克服这个问题......(但这是作弊)..
.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>