I am trying to create an image carousel using CSS where each image appears for four seconds, then cross fades to the next image. I want to include three images that loop.
I found code on another post that seemed close to what I needed, but it was for five images. I thought it would be easy to edit it to just feature three, but I'm not familiar enough with animation timing in CSS and made a mess of it. Now it shows the three images but doesn't show each long enough, and the fade-out / fade-in animations aren't overlapping, leaving a gap between each image.
.crossfade>figure {
animation: imageAnimation 12s linear infinite 0s;
backface-visibility: hidden;
background-size: cover;
background-position: center center;
color: transparent;
height: 100%;
left: 0px;
opacity: 0;
position: absolute;
top: 0px;
width: 100%;
z-index: 0;
}
.crossfade {
height: 450px;
width: 100%;
}
.crossfade>figure:nth-child(1) {
background-image: url('https://picsum.photos/id/237/200/300');
}
.crossfade>figure:nth-child(2) {
animation-delay: 4s;
background-image: url('https://picsum.photos/seed/picsum/200/300');
}
.crossfade>figure:nth-child(3) {
animation-delay: 8s;
background-image: url('https://picsum.photos/200/300?grayscale');
}
@keyframes imageAnimation {
0% {
animation-timing-function: ease-in;
opacity: 0;
}
8% {
animation-timing-function: ease-out;
opacity: 1;
}
17% {
opacity: 1
}
25% {
opacity: 0
}
100% {
opacity: 0
}
}
<div class="crossfade">
<figure></figure>
<figure></figure>
<figure></figure>
</div>
You need to update the percentages. Something like this: