简体   繁体   English

如何修复幻灯片以使点自动运行

[英]how can I fix a slideshow to make dots work automaticaly

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: 这是html文件:

<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: 这是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: 和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. setTimeout正常工作,点不改变其填充颜色。

Put this in your for-loop in showSlides(). 将其放在showSlides()中的for循环中。

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 . 代替使用参数(请参阅1,2,3),可以使用thisevent

 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> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM