我正在学习CSS3动画和过渡,希望您能谅解我的无知。
以下是我尝试做的简化版:
- 我有一个球通过CSS3关键帧“脉动”无限。
- 当我将鼠标悬停在球上时,我希望球变大并保持这样的状态。
- 当我将鼠标移开时,我希望球再次变小并继续“脉动”(所有过渡都需要平滑进行)。
@-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;
}
当鼠标停留在球上时,球保持大的状态,但是当鼠标移开球时,仍然没有平滑的过渡效果。我希望它播放缩小动画,但它没有。
我是否遗漏了什么或者这种效果纯CSS实现目前不可能?
// 相关主题,但对我无效: 停止动画并开始悬停过渡