我正在进入 CSS3 动画和过渡的世界,所以请原谅我的无知。这是我正在尝试做的简化版本:
- 我有一个通过 CSS3 关键帧无限“跳动”的球
- 我希望球变大并在我悬停在它上面时保持这种状态
- 当我将鼠标移开并保持脉动时,我希望球再次变小(当然,所有过渡都需要平滑)。
这是我使用 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;
}
只要鼠标悬停在球上,球就会保持大,但是当鼠标离开球时仍然没有平滑的过渡。我希望它播放收缩动画,但事实并非如此。
我是否遗漏了什么,或者目前仅使用纯 CSS 是不可能实现的?
// 相关线程但对我不起作用:停止动画并在悬停时开始过渡