我目前正在为朋友创建一个网站,而且我有一个我似乎无法弄清楚的问题。我确信这是一个简单的方法调用我已经拥有的。因此,问题似乎是在滑块到达照片末尾后,它只显示一个空白屏幕,并且不会再次重置为开始。我需要调用什么或者需要采用什么方法才能使其正常工作。它在使用箭头本身但不是自动幻灯片时已经有效。提前谢谢。
let sliderImages = document.querySelectorAll(".slide"),
arrowLeft = document.querySelector("#arrow-left"),
arrowRight = document.querySelector("#arrow-right"),
current = 0;
// Clear all images
function reset() {
for (let i = 0; i < sliderImages.length; i++) {
sliderImages[i].style.display = "none";
}
}
//Slides after aprrox 10 seconds
function startTimer() {
setInterval(slideRight, 10000);
}
// Init slider
function startSlide() {
startTimer();
reset();
sliderImages[0].style.display = "block";
}
// Show prev
function slideLeft() {
reset();
sliderImages[current - 1].style.display = "block";
current--;
}
// Show next
function slideRight() {
reset();
sliderImages[current + 1].style.display = "block";
current++;
}
// Left arrow click
arrowLeft.addEventListener("click", function() {
if (current === 0) {
current = sliderImages.length;
}
slideLeft();
});
// Right arrow click
arrowRight.addEventListener("click", function() {
if (current === sliderImages.length - 1) {
current = -1;
}
slideRight();
});
startSlide();
答案 0 :(得分:0)
单击右箭头时,使用以下代码:
if (current === sliderImages.length - 1) {
current = -1;
}
...确保你停止和结束图像,不要试图走得太远。但是你的setInterval()直接调用了slideRight(),它没有防止过远的保护。
答案 1 :(得分:0)
当你调用slideRight()并从数组的末尾掉落时,当前应该重置为零。 //显示下一个 function slideRight(){ 重启(); if(current&gt; sliderImages.length -1){ current = 0; } sliderImages [current] .style.display =&#34; block&#34 ;; 当前++; }
顺便提一下,您可能需要查看bootstrap轮播功能https://getbootstrap.com/docs/4.0/components/carousel/
答案 2 :(得分:0)
使用carousol类型函数结束修复它是我用来替换startSlide函数的代码,谢谢大家的帮助!
kill_task: not killing process (pid=142457/state=Z) with sig 15
02/27/2018 21:42:26.758 M kill_task: killing pid 142458 task 1 with sig 15
02/27/2018 21:42:26.758 M kill_task: not killing process (pid=142460/state=Z) with sig 15
02/27/2018 21:42:26.758 M kill_task: killing pid 142461 task 1 with sig 15
02/27/2018 21:42:26.758 M kill_task: not killing process (pid=142463/state=Z) with sig 15
02/27/2018 21:42:26.758 M kill_task: killing pid 142464 task 1 with sig 15
02/27/2018 21:42:26.758 M kill_task: not killing process (pid=142466/state=Z) with sig 15
02/27/2018 21:42:26.758 M kill_task: killing pid 142467 task 1 with sig 15
02/27/2018 21:42:26.758 M kill_task: not killing process (pid=142469/state=Z) with sig 15
02/27/2018 21:42:26.758 M kill_task: killing pid 142470 task 1 with sig 15
02/27/2018 21:42:26.758 M kill_task: not killing process (pid=142472/state=Z) with sig 15
02/27/2018 21:42:26.758 M kill_task: killing pid 142473 task 1 with sig 15
02/27/2018 21:42:26.758 M kill_task: not killing process (pid=142475/state=Z) with sig 15
02/27/2018 21:42:26.758 M kill_task: killing pid 142476 task 1 with sig 15
02/27/2018 21:42:26.758 M kill_task: not killing process (pid=142478/state=Z) with sig 15
02/27/2018 21:42:26.758 M kill_task: killing pid 142479 task 1 with sig 15
02/27/2018 21:42:26.758 M kill_task: not killing process (pid=142481/state=Z) with sig 15
02/27/2018 21:42:26.758 M kill_task: killing pid 142482 task 1 with sig 15
02/27/2018 21:42:26.758 M kill_task: not killing process (pid=142483/state=Z) with sig 15
02/27/2018 21:42:26.788 M kill_task: not killing process (pid=142442/state=Z) with sig 9
02/27/2018 21:42:26.788 M kill_task: not killing process (pid=142445/state=Z) with sig 9
02/27/2018 21:42:26.788 M kill_task: not killing process (pid=142448/state=Z) with sig 9
02/27/2018 21:42:26.788 M kill_task: not killing process (pid=142451/state=Z) with sig 9
02/27/2018 21:42:26.788 M kill_task: not killing process (pid=142454/state=Z) with sig 9
02/27/2018 21:42:26.788 M kill_task: not killing process (pid=142457/state=Z) with sig 9
02/27/2018 21:42:26.788 M kill_task: not killing process (pid=142460/state=Z) with sig 9
02/27/2018 21:42:26.788 M kill_task: not killing process (pid=142463/state=Z) with sig 9
02/27/2018 21:42:26.788 M kill_task: not killing process (pid=142466/state=Z) with sig 9
02/27/2018 21:42:26.788 M kill_task: not killing process (pid=142469/state=Z) with sig 9
02/27/2018 21:42:26.788 M kill_task: not killing process (pid=142472/state=Z) with sig 9
02/27/2018 21:42:26.788 M kill_task: not killing process (pid=142475/state=Z) with sig 9
02/27/2018 21:42:26.788 M kill_task: not killing process (pid=142478/state=Z) with sig 9
02/27/2018 21:42:26.788 M kill_task: not killing process (pid=142481/state=Z) with sig 9
02/27/2018 21:42:26.788 M kill_task: not killing process (pid=142483/state=Z) with sig 9
02/27/2018 21:42:26.788 M scan_for_terminated: job 2205476.example.com task 1 terminated, sid=121918
02/27/2018 21:42:26.788 M job was terminated
02/27/2018 21:42:26.818 M obit sent to server
02/27/2018 21:42:26.882 M removed job scrip