I try to to display an automatic slideshow. Pictures are changing but the dots below don't work. When I click on any dot, it works, but I want each dot become white simultaneously with slide changing.
Here is html file:
<div class="slideshow-container">
<div class="mySlides fade">
<img src="images/laptop.png" class="welcome__img" style="width:100%">
</div>
<div class="mySlides fade">
<img src="images/laptop.png" class="welcome__img" style="width:100%">
</div>
<div class="mySlides fade">
<img src="images/laptop.png" class="welcome__img" style="width:100%">
</div>
<div style="text-align:center">
<span class="dot" onclick="currentSlide(1)"></span>
<span class="dot" onclick="currentSlide(2)"></span>
<span class="dot" onclick="currentSlide(3)"></span>
</div>
</div>
Here is css:
.slideshow-container {
max-width: 1000px;
position: relative;
margin: auto;
}
/* Hide the images by default */
.mySlides {
display: none;
}
.dot {
cursor: pointer;
height: 10px;
width: 10px;
margin: 0 2px;
background-color: $grey;
border-radius: 50%;
display: inline-block;
transition: background-color 0.6s ease;
}
.active, .dot:hover {
background-color: $white;
}
/* Fading animation */
.fade {
-webkit-animation-name: fade;
-webkit-animation-duration: 1.5s;
animation-name: fade;
animation-duration: 1.5s;
}
@-webkit-keyframes fade {
from {opacity: .4}
to {opacity: 1}
}
@keyframes fade {
from {opacity: .4}
to {opacity: 1}
}
And JS:
var slideIndex = 0;
showSlides();
function showSlides() {
var i;
var slides = document.getElementsByClassName("mySlides");
var dots = document.getElementsByClassName("dot");
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";
for (i = 0; i < dots.length; i++) {
dots[i].className = dots[i].className.replace(" active", "");
}
setTimeout(showSlides, 2000); // Change image every 2 seconds
}
setTimeout works properly exept the dots don't change their fill color.
Put this in your for-loop in showSlides().
if (i === slideIndex) {
dots[i].className = "dot active";
} else {
dots[i].className = "dot";
}
In order to toggle active class at each slide show you may loop through the dots:
var dotActive = document.querySelector('.dot.active');
if (dotActive == null) { // if no dot active...
document.querySelector('.dot').classList.add("active");
} else { // remove current active and set it to next ele
dotActive.classList.remove("active");
(dotActive.nextElementSibling || document.querySelector('.dot')).classList.add("active");
}
Instead to use parameters (refer to 1,2,3) you may use this and event .
var slideIndex = 0; showSlides(); function showSlides() { var i; var slides = document.getElementsByClassName("mySlides"); var dots = document.getElementsByClassName("dot"); 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"; var dotActive = document.querySelector('.dot.active'); if (dotActive == null) { document.querySelector('.dot').classList.add("active"); } else { dotActive.classList.remove("active"); (dotActive.nextElementSibling || document.querySelector('.dot')).classList.add("active"); } setTimeout(showSlides, 2000); // Change image every 2 seconds }
/* Hide the images by default */ .mySlides { display: none; } .dot { cursor: pointer; height: 10px; width: 10px; margin: 0 2px; background-color: grey; border-radius: 50%; display: inline-block; transition: background-color 0.6s ease; } .active, .dot:hover { background-color: white; } /* Fading animation */ .fade { -webkit-animation-name: fade; -webkit-animation-duration: 1.5s; animation-name: fade; animation-duration: 1.5s; } @-webkit-keyframes fade { from {opacity: .4} to {opacity: 1} } @keyframes fade { from {opacity: .4} to {opacity: 1} }
<div class="slideshow-container"> <div class="mySlides fade"> <img src="https://dummyimage.com/100x100/000/fff&text=1" class="welcome__img" style="width:100%"> </div> <div class="mySlides fade"> <img src="https://dummyimage.com/100x100/000/fff&text=2" class="welcome__img" style="width:100%"> </div> <div class="mySlides fade"> <img src="https://dummyimage.com/100x100/000/fff&text=3" class="welcome__img" style="width:100%"> </div> <div style="text-align:center"> <span class="dot" onclick="currentSlide(1)"></span> <span class="dot" onclick="currentSlide(2)"></span> <span class="dot" onclick="currentSlide(3)"></span> </div> </div>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.