I want to animate 5 card / image block one by one then start again from beginning.
Now for some reason its re-starting backwards again.
I have added 5 blocks and total duration of animation is 8 sec, 2sec per block
I am doing keyframing animation 20% to each block then next 20% time to next block and so on.
Please check the codepen of this problem.
@keyframes bounce1 {
0% {
transform: translateY(0px);
}
10% {
transform: translateY(var(--bounce-height));
}
20% {
transform: translateY(0px);
}
40% {
transform: translateY(0px);
}
60% {
transform: translateY(0px);
}
80% {
transform: translateY(0px);
}
100% {
transform: translateY(0px);
}
}
@keyframes bounce2 {
0% {
transform: translateY(0px);
}
10% {
transform: translateY(0px);
}
20% {
transform: translateY(0px);
}
30% {
transform: translateY(var(--bounce-height));
}
40% {
transform: translateY(0px);
}
60% {
transform: translateY(0px);
}
80% {
transform: translateY(0px);
}
100% {
transform: translateY(0px);
}
}
@keyframes bounce3 {
0% {
transform: translateY(0px);
}
10% {
transform: translateY(0px);
}
20% {
transform: translateY(0px);
}
40% {
transform: translateY(0px);
}
50% {
transform: translateY(var(--bounce-height));
}
60% {
transform: translateY(0px);
}
80% {
transform: translateY(0px);
}
100% {
transform: translateY(0px);
}
}
@keyframes bounce4 {
0% {
transform: translateY(0px);
}
10% {
transform: translateY(0px);
}
20% {
transform: translateY(0px);
}
40% {
transform: translateY(0px);
}
60% {
transform: translateY(0px);
}
70% {
transform: translateY(var(--bounce-height));
}
80% {
transform: translateY(0px);
}
100% {
transform: translateY(0px);
}
}
@keyframes bounce5 {
0% {
transform: translateY(0px);
}
10% {
transform: translateY(0px);
}
20% {
transform: translateY(0px);
}
40% {
transform: translateY(0px);
}
60% {
transform: translateY(0px);
}
80% {
transform: translateY(0px);
}
90% {
transform: translateY(var(--bounce-height));
}
100% {
transform: translateY(0px);
}
}
body {
height: 300px;
display: flex;
justify-content: center;
align-items: center;
gap: 16px;
}
.box {
width: 60px;
height: 60px;
background: var(--bg);
border-radius: 4px;
will-change: transform;
}
/*
If you're not seeing the boxes bounce, you
might have "reduce motion" selected in your OS!
*/
@media (prefers-reduced-motion: no-preference) {
.one {
animation:
bounce1 var(--animation-duration)
infinite alternate
cubic-bezier(.2, .65, .6, 1);
}
}
@media (prefers-reduced-motion: no-preference) {
.two {
animation:
bounce2 var(--animation-duration)
infinite alternate
cubic-bezier(.2, .65, .6, 1);
}
}
@media (prefers-reduced-motion: no-preference) {
.three {
animation:
bounce3 var(--animation-duration)
infinite alternate
cubic-bezier(.2, .65, .6, 1);
}
}
@media (prefers-reduced-motion: no-preference) {
.four {
animation:
bounce4 var(--animation-duration)
infinite alternate
cubic-bezier(.2, .65, .6, 1);
}
}
@media (prefers-reduced-motion: no-preference) {
.five {
animation:
bounce5 var(--animation-duration)
infinite alternate
cubic-bezier(.2, .65, .6, 1);
}
}
.box.one {
--bg: slateblue;
--bounce-height: -20px;
--animation-duration: 8000ms;
}
.box.two {
--bounce-height: -20px;
--animation-duration: 8000ms;
--bg: deeppink;
}
.box.three {
--bounce-height: -20px;
--animation-duration: 8000ms;
--bg: dodgerblue;
}
.box.four {
--bounce-height: -20px;
--animation-duration: 8000ms;
--bg: red;
}
.box.five {
--bounce-height: -20px;
--animation-duration: 8000ms;
--bg: yellow;
}
/* These are the delays
.box:nth-child(1) {animation-delay: 0.0s; }
.box:nth-child(2) {animation-delay: 2.0s; }
.box:nth-child(3) {animation-delay: 4.0s; } */
<div class="box one"></div>
<div class="box two"></div>
<div class="box three"></div>
<div class="box four"></div>
<div class="box five"></div>
Thanks,
BT
2
Answers
To fix it, I deleted
alternate
.You don’t need all these animations. You can simplify your code like below: