So I'm making an image changer (you could call it like that), the point is that an image changes after a certain time. I have coded the system already, my problem is that I'm adding a fade-in animation through a class, so what I want is for the image to fade in when it appears. Just look at my code:
The HTML:
<figure id="changing-image"></figure>
The CSS:
.fade-in{
animation: fadeIn 1.5s;
}
@keyframes fadeIn {
0% { opacity: 0; }
100% { opacity: 1; }
}
#changing-image{
background-image: url(../images/asides/Interior.jpg);
background-position: center;
background-repeat: no-repeat;
background-size: cover;
width: 60rem;
height: 50rem;
overflow: hidden;
border-radius: 2px;
}
The JavaScript:
const imagesArray = ["url(img1)", "url(img2)", "url(img3)", "url(img4)"];
const changeImage = i => {
if (i < imagesArray.length) {
setTimeout(function() {
document.getElementById('changing-image').style.backgroundImage = imagesArray[i];
document.getElementById('changing-image').classList.add("fade-in")
changeImage(++i);
}, 2600);
} else if (i === imagesArray.length) {
changeImage(0);
}
document.getElementById('changing-image').classList.remove("fade-in")
}