【问题标题】:Combining keyframes animations into one将关键帧动画合二为一
【发布时间】:2018-03-28 10:30:09
【问题描述】:

我有一个图像,我想“穿过”一个 div,然后在最后水平翻转并以另一种方式返回。我在这里创建了一个代码笔:https://codepen.io/jessiemele/pen/rGQWWE。 tinyWalk 动画在结束时变得非常有弹性,就在它转身并返回开始之前,我假设它是形成击中 div 顶部的图像。我想知道是否有一种方法可以将这两个动画结合起来,只在图像上运行它们,这样我就不必在 div 上运行 tinyWalk。我的代码在这里:

<div class="catapillarBox">
<img src="https://i.imgur.com/0XPhWfE.jpg" class="caterpillar" 
alt="caterpillar drawing" />
</div>
</div>
<div class="blueBox">
<h5>I'm your box</h5>
</div>

CSS:

.blueBox {
  background-color: #1d88eb;
  width: 875px;
  height: 400px;
  margin-top: 125px;
  padding-bottom: 70px;
  margin-top: 150px;
  z-index: 2;
}
img.caterpillar {
  position: absolute;
  top: 125px;
  left:0;
  -webkit-animation: walk 20s forwards infinite linear;
  animation: walk 20s forwards infinite linear;
  z-index: 3;
}
@keyframes walk{
  0% { left: 0; transform: rotateY(0deg);}
  49% {transform: rotateY(0deg);}
  50% {left: 700px; transform: rotateY(180deg);}
  99% {transform: rotateY(180deg);}
  100% {left: 0; transform: rotateY(0deg);
}    
.catapillarBox {
  width: 200px;
  height: 100px;
  -webkit-animation: tinywalk 500ms linear alternate infinite;
  animation: tinywalk 500ms linear alternate infinite;
}
@keyframes tinywalk {
  0%{ transform: rotate(0);}
  25%{ transform: rotate(-1deg);}
  50%{ transform: rotate(1deg);}
  75%{ transform: rotate(-1deg);}
  100%{ transform: rotate(0);}
}

【问题讨论】:

  • 它看起来对我有用(你的代码笔做了你所描述的)。你解决了吗?
  • 嗨,杰克,我刚刚更新了我的问题。我来回翻转并开始工作,但我看到在 div 上而不是图像上放置 tinyWalk 时出现了很多弹跳,在它翻转并回到开始之前,它在结束时非常明显。我想知道有没有一种方法可以将它们组合起来并在图像上添加动画。

标签: css animation css-animations keyframe


【解决方案1】:

杰西卡,我创建了一个代码笔here,应该可以解决您的问题。看起来您对图像的旋转对您的喜好来说太激烈了。我将其编辑为 0.2 度旋转。试试下面的 CSS:

.blueBox {
    background-color: #1d88eb;
    width: 875px;
    height: 400px;
    margin-top: 125px;
    padding-bottom: 70px;
    margin-top: 150px;
    z-index: 2;
}
.catapillarBox {
    width: 200px;
    height: 100px;
   -webkit-animation: tinywalk 500ms linear alternate infinite;
    animation: tinywalk 500ms linear alternate infinite;
}
@keyframes tinywalk {
  0%{ transform: rotate(0);}
  25%{ transform: rotate(-0.2deg);}
  50%{ transform: rotate(0.2deg);}
  75%{ transform: rotate(-0.2deg);}
  100%{ transform: rotate(0);}
}
img.caterpillar {
    position: absolute;
    top: 125px;
    left:0;
    -webkit-animation: walk 20s forwards infinite linear;
    animation: walk 20s forwards infinite linear;
    z-index: 3;
}
@keyframes walk{
   0% { left: 0; transform: rotateY(0deg);}
  49% {transform: rotateY(0deg);}
  50% {left: 700px; transform: rotateY(180deg);}
  99% {transform: rotateY(180deg);}
  100% {left: 0; transform: rotateY(0deg);
}

【讨论】:

  • 感谢 Jack,虽然这看起来更好,但我仍然看到最后的 tinywalk 动画有一点不同,有没有办法在图像上同时运行 walk 和 tinywalk?
  • 让我看看
  • 杰西卡,为了保持相同的解决方案(使用旋转),只要在你的步行动画中移动得更远,旋转就会逐渐变小。再次检查我的代码笔。我已经更新了它here。您可以根据自己的喜好更改旋转度数,但只需随着您步行的深入(达到 50%)减小旋转度数,然后在 50% 到 100% 之间增大旋转度数。
  • 要获得真正流畅的动画,您需要添加更多关键帧。我会把这个留给你。
猜你喜欢
  • 2011-12-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-12-22
  • 2022-11-12
  • 2017-05-30
  • 2021-03-21
  • 1970-01-01
相关资源
最近更新 更多