gpt4 book ai didi

javascript - 如何为JavaScript slider 添加自动幻灯片10秒?

转载 作者:行者123 更新时间:2023-12-02 21:52:12 24 4
gpt4 key购买 nike

我尝试添加 setTimeout(showSlides, 10000);但它不起作用。在页面上不产生延迟的情况下进行自动幻灯片放映的最佳方法是什么?

var slideIndex = 1;
showSlides(slideIndex);

function plusSlides(n) {
showSlides(slideIndex += n);
}

function currentSlide(n) {
showSlides(slideIndex = n);
}

function showSlides(n) {
var i;
var slides = document.getElementsByClassName("mySlides");
var dots = document.getElementsByClassName("dot");
if (n > slides.length) {slideIndex = 1}
if (n < 1) {slideIndex = slides.length}
for (i = 0; i < slides.length; i++) {
slides[i].style.display = "none";
}
for (i = 0; i < dots.length; i++) {
dots[i].className = dots[i].className.replace(" active", "");
}
slides[slideIndex-1].style.display = "block";
dots[slideIndex-1].className += " active";
}

最佳答案

这种默认方法可能会有所帮助。

<!DOCTYPE html>
<html>

<body>
<div style="max-width:500px">
<img class="mySlides" src="your image url" style="width:100%">
<img class="mySlides" src="your image url" style="width:100%">
<img class="mySlides" src="your image url" style="width:100%">
</div>

<script>
var myIndex = 0;
carousel();

function carousel() {
var i;
var x = document.getElementsByClassName("mySlides");
for (i = 0; i < x.length; i++) {
x[i].style.display = "none";
}
myIndex++;
if (myIndex > x.length) {myIndex = 1}
x[myIndex-1].style.display = "block";
setTimeout(carousel, 2000); // Change image every 2 seconds
}
</script>

</body>
</html>

关于javascript - 如何为JavaScript slider 添加自动幻灯片10秒?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60105019/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com