将关键帧动画合并为一个

问题描述:

我有一幅图像,我想“走过”一个div,然后在水平方向翻转并以另一种方式回头。我在这里创建了一个codepen: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);} 
} 
+0

它看起来像是在为我工作(你的codepen做你所描述的)。你解决了这个问题吗? –

+0

嗨,杰克,我刚刚更新了我的问题。我得到了来回,并开始工作,但我看到很多反弹,而不是图像的div上的tinyWalk,它是非常明显的结束之前,它翻转并回到起点。我想知道是否有一种方法可以将它们结合起来,并将动画放在图像上。 –

杰西卡,我创建了一个codepen 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); 
} 
+0

谢谢杰克,虽然这看起来更好,但我仍然看到最后的小动作动画有点不同,有没有办法在图像上同时走路和走路? –

+0

让我看看它 –

+0

杰西卡,以保持相同的解决方案(使用旋转),只是让循环越来越小,因为你进一步走进你的步行动画。再次检查我的codepen。我已经[在这里]更新了它(https://codepen.io/jackmoody/pen/gGQWWX)。你可以根据自己的喜好来改变度数旋转,但是当你走得更远时(走到50%),旋转就会变小,然后当你从50%到100%旋转时,旋转变大。 –