如何在 webkit 中反转 css 关键帧动画?

发布于 2024-12-08 13:02:59 字数 1240 浏览 1 评论 0原文

我有一个控件,我想在关键帧动画之后在两个位置之间转换。有没有办法使用相同的关键帧但相反?另外,有没有办法让动画中途停止并将其反转到开头?

这是我现在所拥有的(我想组合两个关键帧:

@-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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

烈酒灼喉 2024-12-15 13:02:59

如果不直观地看到到目前为止所拥有的内容,很难看出您想要做什么,但请检查动画填充模式。

这将允许您的动画在设置为前进时停止在最后一个关键帧上,目前我相信关键帧在完成后会返回到 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.

゛清羽墨安 2024-12-15 13:02:59

如果将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)

未蓝澄海的烟 2024-12-15 13:02:59

我遇到了同样的问题,并使用 SCSS 生成两个版本的关键帧正常反向。

https://github.com/lichunbin814/scss-utils#with-reverse-version

/* mixin */

@mixin keyframe-reverse( $name, $value) {
  @keyframes #{$name}-rev {
    @each $position,
    $change in $value {
      #{ 100 -$position}% {
        @each $prop,
        $val in $change {
          #{$prop}: #{$val};
        }
      }
    }
  }
}

@mixin keyframe-normal( $name, $value) {
  @keyframes #{$name} {
    @each $position,
    $change in $value {
      #{$position}% {
        @each $prop,
        $val in $change {
          #{$prop}: #{$val};
        }
      }
    }
  }
}

@mixin keyframe-gen( $name, $value, $genReverse) {
  @include keyframe-normal( $name: $name, $value: $value);
  @if ($genReverse) {
    @include keyframe-reverse( $name: $name, $value: $value)
  }
}


/* use */

@include keyframe-gen(
  $name : "fadeIn" ,  
   $value : (
      0: (
            transform: scale(1),
            opacity: 1
      ),
      100: (
            transform: scale(0),
            opacity: 0
      ),
   ) , 
   $genReverse : true 
);

.menu {
  animation-name: fadeIn-rev;
  &.active {
    animation-name: fadeIn;
  }
}

// output css

/*
@keyframes fadeIn {
  0% {
    transform: scale(1);
    opacity: 1;
  }
  100% {
    transform: scale(0);
    opacity: 0;
  }
}

@keyframes fadeIn-rev {
  100% {
    transform: scale(1);
    opacity: 1;
  }
  0% {
    transform: scale(0);
    opacity: 0;
  }
}
*/

I had the same problem, and use SCSS to generate two version of keyframe: normal and reverse.

https://github.com/lichunbin814/scss-utils#with-reverse-version

/* mixin */

@mixin keyframe-reverse( $name, $value) {
  @keyframes #{$name}-rev {
    @each $position,
    $change in $value {
      #{ 100 -$position}% {
        @each $prop,
        $val in $change {
          #{$prop}: #{$val};
        }
      }
    }
  }
}

@mixin keyframe-normal( $name, $value) {
  @keyframes #{$name} {
    @each $position,
    $change in $value {
      #{$position}% {
        @each $prop,
        $val in $change {
          #{$prop}: #{$val};
        }
      }
    }
  }
}

@mixin keyframe-gen( $name, $value, $genReverse) {
  @include keyframe-normal( $name: $name, $value: $value);
  @if ($genReverse) {
    @include keyframe-reverse( $name: $name, $value: $value)
  }
}


/* use */

@include keyframe-gen(
  $name : "fadeIn" ,  
   $value : (
      0: (
            transform: scale(1),
            opacity: 1
      ),
      100: (
            transform: scale(0),
            opacity: 0
      ),
   ) , 
   $genReverse : true 
);

.menu {
  animation-name: fadeIn-rev;
  &.active {
    animation-name: fadeIn;
  }
}

// output css

/*
@keyframes fadeIn {
  0% {
    transform: scale(1);
    opacity: 1;
  }
  100% {
    transform: scale(0);
    opacity: 0;
  }
}

@keyframes fadeIn-rev {
  100% {
    transform: scale(1);
    opacity: 1;
  }
  0% {
    transform: scale(0);
    opacity: 0;
  }
}
*/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文