如何修复幻灯片以使点自动运行
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何修复幻灯片以使点自动运行相关的知识,希望对你有一定的参考价值。
我尝试显示自动幻灯片。图片正在变化,但下面的点不起作用。当我点击任何一个点时,它都有效,但是我希望每个点在幻灯片更改的同时变为白色。
这是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正常工作,但点不会改变其填充颜色。
答案
把它放在showSlides()的for循环中。
if (i === slideIndex) {
dots[i].className = "dot active";
} else {
dots[i].className = "dot";
}
另一答案
为了在每个幻灯片放映时切换活动类,您可以遍历点:
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");
}
而不是使用参数(参见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>
以上是关于如何修复幻灯片以使点自动运行的主要内容,如果未能解决你的问题,请参考以下文章
急,WordPress幻灯片插件RevSlider漏洞如何修复啊?