I'm currently working on creating a carousel using only HTML, CSS, and JS.
While it is functional, it does not perform as smoothly as I had anticipated.
After completing one full round of images, there is an approximate 8-second delay before it starts displaying the images from the beginning again, pausing intermittently.
Moreover, the div containing the background images is set to 100% width and 100vh height. Despite my efforts to adjust properties like bg-repeat
, bg-size
, and bg-position
, I am unable to achieve the desired display on the screen - the images either get cropped with background-size: cover
, or appear too small with background-size: contain
or other settings.
Can someone please review this "working" demo for me? Thank you.
var divi = document.querySelector(".divi");
srcArr = ["https://picsum.photos/id/237/200/300", "https://picsum.photos/id/238/200/300", "https://picsum.photos/id/239/200/300", "https://picsum.photos/id/240/200/300"];
var iter = 0;
setInterval(function() {
if (iter == (srcArr.length)) {
iter = 0;
} else {
divi.style.backgroundImage = "url('" + srcArr[iter] + "')";
iter++;
}
}, 4000);
* {
padding: 0;
margin: 0
}
.divi {
width: 100%;
height: 100vh;
background-image: url("https://picsum.photos/id/240/200/300");
background-repeat: no-repeat;
-webkit-background-size: cover;
background-size: cover;
}
<div class="divi"></div>