* {
padding: 0;
margin: 0;
}
.ground {
display: flex;
height: 500px;
width: 700px;
background-color: darkkhaki;
margin: auto;
margin-top: 70px;
}
.ball {
align-self: flex-end;
height: 50px;
width: 50px;
background-color: brown;
border-radius: 50%;
/* animation: name duration timing-function delay iteration-count direction fill-mode; */
animation: bounce 1s cubic-bezier(0, 0, 0, 0.95) 1s infinite alternate forwards;
-webkit-animation: bounce 1s cubic-bezier(0, 0, 0, 0.95) 1s infinite alternate forwards;
animation: translation 5s ease-out 1s 1 normal forwards;
-webkit-animation: translation 5s ease-out 1s 1 normal forwards;
}
@keyframes bounce {
from {
transform: translateY(0px);
}
to {
transform: translateY(-400px);
}
}
@-webkit-keyframes bounce {
from {
-webkit-transform: translateY(0px);
}
to {
-webkit-transform: translateY(-400px);
}
}
@keyframes translation {
from {
transform: translateX(0px);
}
to {
transform: translateX(650px);
}
}
@-webkit-keyframes translation {
from {
-webkit-transform: translateX(0px);
}
to {
-webkit-transform: translateX(650px);
}
}
<div class="ground">
<div class="ball"></div>
</div>
In this code I am trying to combine two different animations to make a single animation of a ball bouncing with translation, but the animations are not playing simultaneously, instead the one which is assigned later in the code is played.
I have tried separating each animation property as such:
animation-name: bounce, translation;
animation-duration: 5s;
animation-timing-function: cubic-bezier(0, 0, 0, 0.95), ease-out;
animation-delay: 1s;
animation-iteration-count: infinite, 1;
animation-direction: alternate, normal;
animation-fill-mode: forwards;
but it still doesnt work, in this case i have mentioned translation animation after the bounce animation so only translation animation is playing.
Please help me out on this, as I want to play both animations simultaneously so it looks like the ball is bouncing and moving in a direction.
3
Answers
To achieve the effect of a ball bouncing and moving horizontally at the same time, you need to combine both the bounce and translation animations correctly. The issue in your original code is that you are defining two separate animation properties for .ball, and the latter one is overriding the former. To combine these animations, you need to list them together in the animation shorthand property or define each animation property separately for both animations.
Here’s how you can modify your CSS to achieve the desired effect:
Key Points:
bounce and translation animations. Each animation is defined with its
own duration, timing function, delay, iteration count, direction, and
fill mode.
up for both animations. Here, they are defined from 0% to 100%.
transform, you need to ensure they don’t conflict. In this case,
since one is translating in the X direction and the other in the Y
direction, they can be combined without issues.
By setting up your CSS in this way, both animations should run simultaneously, creating the effect of a ball bouncing and moving horizontally at the same time.
The problem is that the two
@keyframe
apply thetransform
property, so only one can ever apply.You could consider having two elements where each
@keyframe
is applied individually:The previous answers correctly identify the problems with your code (combining animations into one
animation
property and the inability to assign thetransform
property twice), but to avoid adding an extra element, you can try using theleft
property for your translation animation instead oftransform
: