同时运行多个CSS动画效果

4
为了用我的动画技能吓唬Pixar的家伙们,我正在尝试使用CSS实现行走效果...
不幸的是,我无法同时使用两种不同的动画效果,我希望steps以可变速率旋转到walkRight过渡中。
这是我目前的尝试:

CSS

.wrapper {
    position: absolute;
    width: 100px;
    height: 100px; 
    right: 0;
    animation-name: walkRight;
    animation-iteration-count: 1;
    animation-timing-function: ease-out;
    animation-duration: 10s;
} 

.hulk {
  -webkit-animation: steps 10s linear 0s;
}

@keyframes walkRight {
  0% {
    transform: translateX(-400px);
  }
  100% {
    transform: translateX(0);
  }
}

@-webkit-keyframes steps {
  0% {
    -webkit-transform: rotate(0deg);
  }
  25% {
    -webkit-transform: rotate(20deg);
  }
  50% {
    -webkit-transform: rotate(0deg);
  }
  75% {
    -webkit-transform: rotate(-20deg);
  }
  100% {
    -webkit-transform: rotate(0deg);
  }
}

这是一个例子 JsFiddle

我有点迷失了。这个角色面向我们,所以不确定步骤如何产生“行走”效果。 - Harry
两个动画在Chrome和Firefox中同时运行。您是否使用不识别Webkit的浏览器?为什么将一个变换指定为Webkit,而将另一个指定为通用? - Edward D
@EdwardD 我希望steps移动得更快,独立于transitionX。 - Matt D. Webb
2个回答

2
您可以尝试以下方法:
  • hulk 类上使用 animation-iteration-count: 10,并将其持续时间设置为 1s(因为 walkRight 的持续时间为 10s),这意味着在行走过程中将应用 walk 效果 10 次。
  • 为确保浏览器正确渲染您的动画,请在所有属性前加上 -webkit- 前缀。您可以使用 autoprefixer(或类似工具)自动完成此操作。

 .wrapper {
  position: absolute;
  width: 100px;
  height: 100px;
  right: 0;
  -webkit-animation-name: walkRight;
      animation-name: walkRight;
  -webkit-animation-timing-function: ease-out;
      animation-timing-function: ease-out;
  -webkit-animation-duration: 10s;
      animation-duration: 10s;
}
.hulk {
  -webkit-animation: steps 1s linear 0s;
  -webkit-animation-iteration-count: 10;
      animation-iteration-count: 10;
}
@-webkit-keyframes walkRight {
  0% {
-webkit-transform: translateX(-400px);
        transform: translateX(-400px);
  }
  100% {
-webkit-transform: translateX(0);
        transform: translateX(0);
  }
}
@keyframes walkRight {
  0% {
-webkit-transform: translateX(-400px);
        transform: translateX(-400px);
  }
  100% {
-webkit-transform: translateX(0);
        transform: translateX(0);
  }
}
@-webkit-keyframes steps {
  0% {
-webkit-transform: rotate(0deg);
  }
  25% {
-webkit-transform: rotate(20deg);
  }
  50% {
-webkit-transform: rotate(0deg);
  }
  75% {
-webkit-transform: rotate(-20deg);
  }
  100% {
-webkit-transform: rotate(0deg);
  }
}
<div class="wrapper">
  <img class="hulk" width="100px" src="http://vignette3.wikia.nocookie.net/heroup/images/4/4b/Thing_full_body.png/revision/latest?cb=20120117152657">
</div>


0

您可以在 steps 动画上使用 animation-iteration-count 并设置较短的 duration。您只需要匹配 walksteps 的结束时间,使其重复自身 n 次,因此在这种情况下,如果持续时间为1秒,则重复次数约为9。

.wrapper {
  position: absolute;
  width: 100px;
  height: 100px;
  right: 0;
  animation-name: walkRight;
  animation-iteration-count: 1;
  animation-timing-function: ease-out;
  animation-duration: 10s;
}
.hulk {
  -webkit-animation: steps 1s linear 0s;
  animation-iteration-count: 9;
}
@keyframes walkRight {
  0% {
    transform: translateX(-400px);
  }
  100% {
    transform: translateX(0);
  }
}
@-webkit-keyframes steps {
  0% {
    -webkit-transform: rotate(0deg);
  }
  25% {
    -webkit-transform: rotate(20deg);
  }
  50% {
    -webkit-transform: rotate(0deg);
  }
  75% {
    -webkit-transform: rotate(-20deg);
  }
  100% {
    -webkit-transform: rotate(0deg);
  }
}
<div class="wrapper">
  <img class="hulk" width="100px" src="http://vignette3.wikia.nocookie.net/heroup/images/4/4b/Thing_full_body.png/revision/latest?cb=20120117152657">
</div>


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接