我正在进入CSS3动画和过渡的世界,所以请原谅我的无知 . 这是我正在尝试做的简化版本:
-
我通过CSS3关键帧无限地拥有一个"pulsates"球
-
当我将鼠标悬停在球上时,我希望球变得更大并保持这种状态
-
当我将鼠标移开并保持脉动时,我希望球再次变小(当然,所有过渡都需要平滑) .
这是我使用CSS3动画和过渡的混合(到目前为止在Chrome上测试这个,因此webkit前缀):
@-webkit-keyframes pulsate {
from {
-webkit-transform: scale(0.7);
}
to {
-webkit-transform: scale(0.8);
}
}
.ball {
background: blue;
width: 100px;
height: 100px;
border-radius: 50%;
transition: all 1s;
-webkit-transform: scale(0.7);
-webkit-transform-origin: center center;
-webkit-animation-duration: 800ms;
-webkit-animation-name: pulsate;
-webkit-animation-iteration-count: infinite;
-webkit-animation-direction: alternate;
-webkit-animation-timing-function: ease-in-out;
}
.ball:hover {
-webkit-transform: scale(2);
-webkit-animation-play-state: paused; /* transition works but gets reset at the end*/
/*-webkit-animation: 0;*/ /* transition works only one time, and no smooth transition on mouse out */
}
结果非常接近但是一旦球在悬停时完成扩展,它就会突然变小(不明白为什么) . 我也尝试通过 -webkit-animation: 0;
禁用动画而不是暂停它,但它也不能正常工作 .
我尝试了一种不同的方法,它只使用关键帧(没有转换),试图在悬停时调用另一个关键帧集:
@-webkit-keyframes pulsate {
from {
-webkit-transform: scale(0.7);
}
to {
-webkit-transform: scale(0.8);
}
}
@-webkit-keyframes expand {
to {
-webkit-transform: scale(2);
}
}
@-webkit-keyframes shrink {
to {
-webkit-transform: scale(0.7);
}
}
.ball {
background: blue;
width: 100px;
height: 100px;
border-radius: 50%;
transition: all 2s;
-webkit-transform: scale(0.7);
-webkit-transform-origin: center center;
-webkit-animation-duration: 800ms, 800ms;
-webkit-animation-name: shrink, pulsate;
-webkit-animation-iteration-count: 1, infinite;
-webkit-animation-direction: normal, alternate;
-webkit-animation-timing-function: ease-in-out, ease-in-out;
}
.ball:hover {
-webkit-animation-name: expand;
-webkit-animation-iteration-count: 1;
-webkit-animation-direction: normal;
-webkit-animation-fill-mode: forwards;
}
只要鼠标位于球上,球就会保持很大但当鼠标移离球时仍然没有平滑过渡 . 我希望它能够播放收缩动画,但事实并非如此 .
Am I missing something or this is impossible to implement with just pure CSS at the moment?
//相关主题,但对我不起作用:Stop animation and start transition on hover
2 回答
您需要添加动画延迟以允许转换完成,因为它会在动画开始时恢复为
scale(.7)
. Updated jsFiddleEDIT
我意识到我在这里发布的答案并不完全正确 . 确实,延迟激活了从大回到小回的过渡,但是如果你在它的扩展时将其悬停在脉冲球上,它会在动画到大规模之前跳回到它的0值.7 .
Updated Demo
我提出了一个修复程序,只是使用一些javascript来修复它基于this article . 你必须稍微改变一下CSS,但结果并不是很明显 . 这是更新的代码
只需更新此CSS规则,我已添加From&To - 在Expand&Shrink中: