简体   繁体   English

带有 setInterval function 的自动图像 slider 不起作用

[英]Automatic image slider with setInterval function is not working

Third photo of slider doesn't apper on slide. slider 的第三张照片没有出现在幻灯片上。 After a while slide fails completely, photos are changing rapidly.一段时间后幻灯片完全失败,照片正在迅速变化。 After the slide fail, when photos are changing rapidly, third photo appears sometimes.幻灯片失败后,当照片快速变化时,有时会出现第三张照片。 But I couldn't figure out why it doesn't appears at first and why slide is getting faster.但我不明白为什么它一开始没有出现,为什么幻灯片越来越快。 How can I fix this problem?我该如何解决这个问题?

 let sliderImages = document.querySelectorAll(".slide"); current = 0; function reset() { for(let i = 0; i < sliderImages.length; i++) { sliderImages[i].style.display = "none"; } } function startSlide() { reset(); if(current == sliderImages.length) { current = 0; } sliderImages[current].style.display = "block"; current++; setInterval(startSlide, 2000); } startSlide();
 body, #slider, .wrap, .slide-content { margin: 0; padding: 0; width: 100%; height: 100vh; overflow-x: hidden; }.wrap { position: relative; }.slide { background-size: cover; background-position: center; background-repeat: no-repeat; }.slide1 { background-image: url("https://images.pexels.com/photos/3153198/pexels-photo-3153198.jpeg?auto=compress&cs=tinysrgb&h=750&w=1260"); }.slide2 { background-image: url("https://images.pexels.com/photos/3153208/pexels-photo-3153208.jpeg?auto=compress&cs=tinysrgb&h=750&w=1260") }.slide3 { background-image: url("https://images.pexels.com/photos/1036641/pexels-photo-1036641.jpeg?auto=compress&cs=tinysrgb&h=750&w=1260") }
 <div class="wrap"> <div id="slider"> <div class="slide slide1"> <div class="slide-content"> </div> </div> <div class="slide slide2"> <div class="slide-content"> </div> </div> <div class="slide slide3"> <div class="slide-content"> </div> </div> </div> </div>

You problem is everytime you run the function a new interval is going to be set, so you will end up with a bunch of them running at the same time.你的问题是每次你运行 function 时都会设置一个新的间隔,所以你最终会同时运行一堆。 You just need to set it once, and then the function will be called every two seconds appropriately.您只需设置一次,然后每隔两秒适当地调用一次 function。 I modified your example:我修改了你的例子:

 let sliderImages = document.querySelectorAll(".slide"); current = 0; function reset() { for(let i = 0; i < sliderImages.length; i++) { sliderImages[i].style.display = "none"; } } function startSlide() { reset(); if(current == sliderImages.length) { current = 0; } sliderImages[current].style.display = "block"; current++; } setInterval(startSlide, 2000);
 body, #slider, .wrap, .slide-content { margin: 0; padding: 0; width: 100%; height: 100vh; overflow-x: hidden; }.wrap { position: relative; }.slide { background-size: cover; background-position: center; background-repeat: no-repeat; }.slide1 { background-image: url("https://images.pexels.com/photos/3153198/pexels-photo-3153198.jpeg?auto=compress&cs=tinysrgb&h=750&w=1260"); }.slide2 { background-image: url("https://images.pexels.com/photos/3153208/pexels-photo-3153208.jpeg?auto=compress&cs=tinysrgb&h=750&w=1260") }.slide3 { background-image: url("https://images.pexels.com/photos/1036641/pexels-photo-1036641.jpeg?auto=compress&cs=tinysrgb&h=750&w=1260") }
 <div class="wrap"> <div id="slider"> <div class="slide slide1"> <div class="slide-content"> </div> </div> <div class="slide slide2"> <div class="slide-content"> </div> </div> <div class="slide slide3"> <div class="slide-content"> </div> </div> </div> </div>

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

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