I want to create a slideShow and it almost works well but when using the flash to continue to one direction when it comes to end of that direction and starts from the first dot the same flash for that direction doesn't work anymore. would you please let me know what the problem is? here is 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">
<title>slide-show</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<section id="container">
<img src="1.jpeg" alt="" class="img" id="1">
<img src="2.jpeg" alt="" class="img" id="2">
<img src="3.jpeg" alt="" class="img" id="3">
<img src="4.webp" alt="" class="img" id="4">
</section>
<div id="page">
<span onclick="plusSlides(-1);"><</span>
<span onclick="plusSlides(1)">></span>
</div>
<section id="dot">
<a class="dots" onclick="currentSlide(0)"></a>
<a class="dots" onclick="currentSlide(1)"></a>
<a class="dots" onclick="currentSlide(2)"></a>
<a class="dots" onclick="currentSlide(3)"></a>
</section>
</body>
<script src="script.js"></script>
</html>
css:
#container {
display: flex;
flex-direction: column;
width: 90%;
height: 80vh;
overflow: hidden;
margin: 0 auto;
}
#dot {
width: 20%;
height: 5vh;
margin: 2vh 0;
display: flex;
justify-content: space-around;
margin: 10vh auto;
}
a {
width: 2vw;
height: 2.5vh;
background-color: #b0b0b0;
border-radius: 100%;
transition: background-color .5s ease;
}
.img {
display: none;
}
a:hover {
cursor: pointer;
}
#page {
margin-left: 9%;
width: 80%;
position: absolute;
display: flex;
justify-content: space-between;
top: 32vh;
}
span:hover {
cursor: pointer;
}
span {
background-color: #1d2129;
color: aliceblue;
font-size: larger;
padding: 2%;
border-radius: 50%;
}
js:
let index = 0;
slideShow(index);
function plusSlides(n) {
slideShow(index += n);
};
function currentSlide(n) {
slideShow(index = n);
};
function slideShow(index) {
let i;
let img = document.querySelectorAll("img");
let dots = document.querySelectorAll(".dots");
if (index > 3) {
index = 0;
}
if (index < 0) {
index = 3;
}
for (i = 0; i < img.length; i++) {
img[i].classList.add("img");
}
for (i = 0; i < dots.length; i++) {
dots[i].style.backgroundColor = "#b0b0b0";
}
img[index].classList.remove('img');
dots[index].style.background = "#686868";
};