我尝试显示自动幻灯片放映。图片在变化,但是下面的点不起作用。当我单击任何点时,它都可以工作,但是我希望每个点同时变幻并同时变白。
这是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>
这是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}
}
和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正常工作,点不改变其填充颜色。
答案 0 :(得分:1)
将其放在showSlides()的for循环中。
Task.FromResult
答案 1 :(得分:1)
要在每张幻灯片上切换活动班级,您可以在圆点之间循环浏览:
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");
}
您可以使用 this 和 event 来代替使用参数(请参阅1,2,3)。
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>