I'm getting a low CLS (layout shift) score on google page speed test, and I figured out it is because of how my slider loads. When the page is loading (mostly on slow connections) the slider's images are all loaded below eachother, when the page is done loading and the javascript loads, only then are the rest of the images hidden and starts to slide.
I created the following slider from a tutorial. I have no idea how to solve this problem. I would think the best way would be to try and hide all the other images (except the first one) until the javascript is loaded. Is there a way to achieve this with javascript? Or is there a better solution
What would be the best way to approach this problem? Here is my slider's code:
var slideIndex = 0;
showSlides();
function showSlides() {
var i;
var slides = document.getElementsByClassName("single-slide");
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, 6000);
}