1 回答
TA贡献1824条经验 获得超5个赞
具有两个var声明会创建两个单独的间隔。只需摆脱第二个,您就应该做好了。
var slides = document.querySelectorAll(".slide");
var dots = document.querySelectorAll(".dot");
function removeClass() {
for (var i = 0; i < slides.length; i++) {
slides[i].classList.remove('active');
}
}
function removeNext() {
for (var i = 0; i < slides.length; i++) {
slides[i].classList.remove('next');
}
}
function slideshow() {
currentSlide = document.querySelector(".active");
nextSlide = currentSlide.nextElementSibling;
if (nextSlide != null) {
removeClass();
nextSlide.classList.add('next');
nextSlide.classList.add('active');
} else {
removeClass();
slides[0].classList.add('next');
slides[0].classList.add('active');
}
removeNext();
}
var slideDelay = setInterval(slideshow, 6000);
document.addEventListener("click", function (event) {
if (event.target.className == "dot") {
removeClass();
var dotAttrValue = event.target.getAttribute('data-slide-to');
slides[dotAttrValue - 1].classList.add('active');
clearInterval(slideDelay);
slideDelay = setInterval(slideshow, 6000);//Var removed from here.
}
});
添加回答
举报