I am trying to create a fade in and fade out effect on my photos in html. How do I call the keyframes in css to javascript when the image is displayed?
I have to use javascript as part of the requirements for the slideshow
It seems like i have to insert .classlist.add?
var slideIndex = 0;
showSlides();
function showSlides() {
var i;
var slides = document.getElementsByClassName("fade");
for (i = 0; i < slides.length; i++) {
slides[i].style.display = "none";
}
slideIndex++;
if (slideIndex > slides.length) {
slideIndex = 1
}
slides[slideIndex - 1].style.display = "block";
setTimeout(showSlides, 5000);
}
@keyframes fadein {
from {
opacity: .4
}
to {
opacity: 1
}
}
@keyframes fadeout {
from {
opacity: .4
}
to {
opacity: 1
}
}
<!-- Images used for slideshow -->
<div class="fade">
<figure><img class="img-fluid" src=https://via.placeholder.com/150
C/O https://placeholder.com/"></figure>
</div>
<div class="fade">
<figure><img class="img-fluid" src="https://via.placeholder.com/150
C/O https://placeholder.com/"> </figure>
</div>
<div class="fade">
<figure><img class="img-fluid" src="https://via.placeholder.com/150
C/O https://placeholder.com/"></figure>
</div>
<div class="fade">
<figure><img class="img-fluid" src="https://via.placeholder.com/150
C/O https://placeholder.com/"></figure>
</div>
<div class="fade">
<figure><img class="img-fluid" src="https://via.placeholder.com/150
C/O https://placeholder.com/eg"></figure>
</div>
</div>
You don't need keyframes for this:
UPDATE: ES5 version at OP's request: