As per the title, I am trying to create a JS script that cycles through a series of images in the same <div>
. The images will be cycled through to create a smooth animation. So far, I have this. The images cycle through but without a smooth transition.
var slideIndex = 0;
showSlides();
function showSlides() {
var i;
var slides = document.getElementsByClassName("mySlides");
for (i = 0; i < slides.length; i++) {
slides[i].style.display = "none";
slides[i].style.opacity = "0";
}
slideIndex++;
if (slideIndex > slides.length) {
slideIndex = 1
}
slides[slideIndex - 1].style.display = "block";
slides[slideIndex - 1].style.opacity = "1";
setTimeout(showSlides, 2500);
// Add a transition to the images.
slides[slideIndex - 1].style.transition = "all 2.50s";
// Add a delay to the images.
slides[slideIndex - 1].style.transitionDelay = "2.50s";
}
<div class="p-centered">
<img src="https://picsum.photos/200?1" class="img-responsive mySlides" alt="">
<img src="https://picsum.photos/200?2" class="img-responsive mySlides" alt="">
<img src="https://picsum.photos/200?3" class="img-responsive mySlides" alt="">
<img src="https://picsum.photos/200?4" class="img-responsive mySlides" alt="">
<img src="https://picsum.photos/200?5" class="img-responsive mySlides" alt="">
</div>