HTML:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link
rel="stylesheet"
href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css"
/>
<link rel="stylesheet" href="style.css" />
<title>Carousel</title>
</head>
<body>
<div class="container">
<div class="image fade">
<img src="./img/Lam1.jpeg" alt="Lamborghini Image" width="100%" />
</div>
<div class="image fade">
<img src="./img/Lam2.jpeg" alt="Lamborghini Image" width="100%" />
</div>
<div class="image fade">
<img src="./img/Lam3.jpeg" alt="Lamborghini Image" width="100%" />
</div>
<div class="image fade">
<img src="./img/Lam4.jpeg" alt="Lamborghini Image" width="100%" />
</div>
<div class="image fade">
<img src="./img/Lam5.jpeg" alt="Lamborghini Image" width="100%" />
</div>
<div class="image fade">
<img src="./img/Lam6.webp" alt="Lamborghini Image" width="100%" />
</div>
<div class="image fade">
<img src="./img/Lam7.webp" alt="Lamborghini Image" width="100%" />
</div>
<div class="image fade">
<img src="./img/Lam8.jpeg" alt="Lamborghini Image" width="100%" />
</div>
<i class="fa fa-chevron-left prev" aria-hidden="true"></i>
<i class="fa fa-chevron-right next" aria-hidden="true"></i>
<div class="dots">
<span class="dot"></span>
<span class="dot"></span>
<span class="dot"></span>
<span class="dot"></span>
<span class="dot"></span>
<span class="dot"></span>
<span class="dot"></span>
<span class="dot"></span>
</div>
</div>
</body>
</html>
JavaScript:
// Selectors
const images = document.querySelectorAll(".image");
const previous = document.querySelector(".prev");
const next = document.querySelector(".next");
const dots = document.querySelectorAll(".dot");
// Event Listeners
previous.addEventListener("click", showPrevImg);
next.addEventListener("click", showNextImg);
//Functions
let index = 0;
showImg();
function showImg() {
for (let i = 0; i < images.length; i++) {
if (i) {
images[i].style.display = "block";
dots[i].classList.add("active");
} else {
images[i].style.display = "none";
dots[i].classList.remove("active");
}
console.log(images[i]);
}
}
function showPrevImg() {
index--;
if (index < 0) {
index = images.length - 1;
}
showImg();
}
function showNextImg() {
index++;
if (index > images.length - 1) {
index = 0;
}
showImg();
}
console.log(showImg());
CSS:
.image {
display: none;
}
I am trying show my images through the javascript code after a display of none in css and also use previous and next arrows to skip through the images but my images are not showing to begin with. Any help is appreciated.