I am facing an issue where the third photo in the slider does not appear initially. Eventually, the slide fails completely, causing the photos to change rapidly. Strangely, after the slide fails, the third photo sometimes appears sporadically. I am puzzled as to why this happens and how to fix it. Can you offer any solutions?
let sliderImages = document.querySelectorAll(".slide");
current = 0;
function reset() {
for(let i = 0; i < sliderImages.length; i++) {
sliderImages[i].style.display = "none";
}
}
function startSlide() {
reset();
if(current == sliderImages.length) {
current = 0;
}
sliderImages[current].style.display = "block";
current++;
setInterval(startSlide, 2000);
}
startSlide();
body, #slider, .wrap, .slide-content {
margin: 0;
padding: 0;
width: 100%;
height: 100vh;
overflow-x: hidden;
}
.wrap {
position: relative;
}
.slide {
background-size: cover;
background-position: center;
background-repeat: no-repeat;
}
.slide1 {
background-image: url("https://images.pexels.com/photos/3153198/pexels-photo-3153198.jpeg?auto=compress&cs=tinysrgb&h=750&w=1260");
}
.slide2 {
background-image: url("https://images.pexels.com/photos/3153208/pexels-photo-3153208.jpeg?auto=compress&cs=tinysrgb&h=750&w=1260")
}
.slide3 {
background-image: url("https://images.pexels.com/photos/1036641/pexels-photo-1036641.jpeg?auto=compress&cs=tinysrgb&h=750&w=1260")
}
<div class="wrap">
<div id="slider">
<div class="slide slide1">
<div class="slide-content">
</div>
</div>
<div class="slide slide2">
<div class="slide-content">
</div>
</div>
<div class="slide slide3">
<div class="slide-content">
</div>
</div>
</div>
</div>