【发布时间】:2013-08-22 09:19:27
【问题描述】:
我正在进入 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;
}
只要鼠标悬停在球上,球就会保持大,但当鼠标离开球时仍然没有平滑的过渡。我希望它播放 shrink 动画,但它没有。
我是否遗漏了什么,或者目前仅使用纯 CSS 无法实现?
// 相关线程但对我不起作用:Stop animation and start transition on hover
【问题讨论】:
-
在您的第一个小提琴中 - 如果您在过渡上添加一点延迟,它将始终在悬停时过渡(不仅在第一次):transition: all 1s 0.01; - jsfiddle.net/danield770/ntTUt/1
-
有趣!你能解释一下它为什么起作用吗?我仍然需要在跳动时从任意状态开始转换,而不是像现在这样从 scale(0.7) 开始,然后在鼠标移出时平滑地转换回来
标签: css hover css-transitions css-animations