如何在 webkit 中反转 css 关键帧动画?
我有一个控件,我想在关键帧动画之后在两个位置之间转换。有没有办法使用相同的关键帧但相反?另外,有没有办法让动画中途停止并将其反转到开头?
这是我现在所拥有的(我想组合两个关键帧:
@-webkit-keyframes explode {
0% {
-webkit-transform: scale(1.0) rotate(0deg) translate(0px, 0px);
}
33% {
-webkit-transform: scale(2.0) translate(100px, -150px);
}
67% {
-webkit-transform: scale(2.0) translate(200px, -250px);
}
100% {
-webkit-transform: scale(1.0) translate(-15px, -15px);
}
}
@-webkit-keyframes explodeBack {
0% {
-webkit-transform: scale(1.0) translate(-15px, -15px);
}
67% {
-webkit-transform: scale(2.0) translate(100px, -150px);
}
100% {
-webkit-transform: scale(1.0) rotate(0deg) translate(0px, 0px);
}
}
.leftArrowAnimateForward{
-webkit-animation-name: explode;
-webkit-animation-duration: 3s;
-webkit-animation-timing-function: linear;
-webkit-animation-direction:normal; /* Safari and Chrome */
-webkit-transform: scale(1.0) translate(-15px, -15px);
}
.leftArrowAnimateBackward{
-webkit-animation-name: explodeBack;
-webkit-animation-duration: 3s;
-webkit-animation-timing-function: linear;
-webkit-animation-direction:alternate;
-webkit-transform: scale(1.0) translate(0px, 0px);
}
I have a control that I want to transition between two locations following a keyframe animation. Is there a way to use the same keyframe but in reverse? Also, is there a way to stop the animation, halfway and reverse it to the beginning?
Here is what I have now (and I want to combine the two keyframes:
@-webkit-keyframes explode {
0% {
-webkit-transform: scale(1.0) rotate(0deg) translate(0px, 0px);
}
33% {
-webkit-transform: scale(2.0) translate(100px, -150px);
}
67% {
-webkit-transform: scale(2.0) translate(200px, -250px);
}
100% {
-webkit-transform: scale(1.0) translate(-15px, -15px);
}
}
@-webkit-keyframes explodeBack {
0% {
-webkit-transform: scale(1.0) translate(-15px, -15px);
}
67% {
-webkit-transform: scale(2.0) translate(100px, -150px);
}
100% {
-webkit-transform: scale(1.0) rotate(0deg) translate(0px, 0px);
}
}
.leftArrowAnimateForward{
-webkit-animation-name: explode;
-webkit-animation-duration: 3s;
-webkit-animation-timing-function: linear;
-webkit-animation-direction:normal; /* Safari and Chrome */
-webkit-transform: scale(1.0) translate(-15px, -15px);
}
.leftArrowAnimateBackward{
-webkit-animation-name: explodeBack;
-webkit-animation-duration: 3s;
-webkit-animation-timing-function: linear;
-webkit-animation-direction:alternate;
-webkit-transform: scale(1.0) translate(0px, 0px);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果不直观地看到到目前为止所拥有的内容,很难看出您想要做什么,但请检查动画填充模式。
这将允许您的动画在设置为前进时停止在最后一个关键帧上,目前我相信关键帧在完成后会返回到 0。
玩一玩,如果成功请告诉我们。
It's hard to see what your trying to do without visually seeing what you have so far, but checkout animation-fill-mode.
This will allow your animation to stop on the last keyframe when set to forward, where at the moment I believe the keyframes go back to 0 when they are finished.
Have a play and let us know if your successful.
如果将animation-iteration-count:2与animation-direction:alternate和动画长度为负数的animation-delay结合起来,您可以做您想做的第一件事,您将获得要播放的动画正好相反一次。 (它基本上跳到反向动画并开始在那里播放。)
您无法使用纯 CSS 动画做第二件事,除非您定义第二组关键帧并使用 JS 在类之间切换(或悬停或其他)
You can do the first thing you want to do if you combine animation-iteration-count: 2, with an animation-direction: alternate, and a animation-delay that is negative the length of your animation, you will get an animation to play in reverse exactly once. (It basically skips ahead to the reverse direction animation and starts playing there.)
You can't do the second thing you want to do with pure CSS animation, unless you define a second set of keyframes and toggle between the classes with JS (or hover or whatever)
我遇到了同样的问题,并使用 SCSS 生成两个版本的关键帧:
正常
和反向。
https://github.com/lichunbin814/scss-utils#with-reverse-version
I had the same problem, and use SCSS to generate two version of keyframe:
normal
andreverse.
https://github.com/lichunbin814/scss-utils#with-reverse-version