简体   繁体   English

使用数组和for循环的JavaScript轮播:为什么slideIndex在第一个循环后被“卡住”?

[英]JavaScript carousel using array and for-loop: Why does slideIndex get “stuck” after first loop?

I'm trying to create a javascript carousel of scrolling images without jQuery, using an array and a for-loop. 我正在尝试使用数组和for循环创建一个无需jQuery即可滚动图像的javascript轮播。

At the end of the 4-object array of images, the "next button" returns to array[slideIndex=0]. 在4个对象的图像数组的末尾,“下一个按钮”返回到array [slideIndex = 0]。

Similarly, when you hit the "previous button" on the first object in the array, it returns the last object in the array. 同样,当您在数组中的第一个对象上单击“上一个按钮”时,它将返回数组中的最后一个对象。

This function seems to almost work with the code I have right now. 该功能似乎几乎可以与我现在拥有的代码一起使用。 However, it only works once. 但是,它只能运行一次。 After skipping backwards or forwards to the last or first object in the array, it doesn't continue to scroll in the intended direction. 向后或向前跳转到数组的最后一个或第一个对象后,它不会继续按预期的方向滚动。

Can you please tell me why the slideIndex doesn't continue to loop with the plusDivs() function after the first skip? 您能告诉我为什么在第一次跳过之后slideIndex不会继续与plusDivs()函数一起循环吗?

 let slides_array = document.getElementsByClassName("mySlides"); let slideIndex = 0; showDivs(slideIndex); function plusDivs(n) { slideIndex += n; showDivs(slideIndex); } function showDivs(slideIndex) { let i; if (slideIndex > (slides_array.length - 1)) { slideIndex = 0; }; if (slideIndex < 0) { slideIndex = (slides_array.length - 1); }; for (i=0; i<(slides_array.length); i++) { slides_array[i].style.display = "none"; } slides_array[slideIndex].style.display = "block"; } 
 <section class = "Slideshow"> <div id="slide1" class="mySlides"> <img src="http://cdn3-www.dogtime.com/assets/uploads/gallery/goldador-dog-breed-pictures/puppy-1.jpg"> </div> <div id="slide2" class="mySlides"> <img src="https://www.cesarsway.com/sites/newcesarsway/files/styles/large_article_preview/public/Shake-shiver-and-tremble-Why-dogs-do-it.jpg?itok=yvOUgQeL"> </div> <div id="slide3" class="mySlides"> <img src="https://i.kinja-img.com/gawker-media/image/upload/s--kx3d-Wgg--/c_scale,fl_progressive,q_80,w_800/ol9ceoqxidudap8owlwn.jpg"> </div> <div id="slide4" class="mySlides"> <img src="http://cdn1-www.dogtime.com/assets/uploads/gallery/korean-jindo-dog-breed-pictures/puppy-1_680-453.jpg"> </div> </section> <!-- Slideshow Buttons --> <button class="button" onclick="plusDivs(-1)"> &#10094; </button> <button class="button" onclick="plusDivs(+1)"> &#10095; </button> 

Pardon (and please correct) me if I'm not using the correct technical terms -- I'm trying to describe my current issue as best as I can, but maybe this isn't the best way to describe the problem. 如果我没有使用正确的技术术语,请原谅(并请纠正),我正在尽力描述当前问题,但这也许不是描述问题的最佳方法。 Please advise. 请指教。

As you have a global variable slideIndex , you should not pass it as a function argument, as that creates a local variable with the same name, but any reassignment to it will not affect the global variable. 由于具有全局变量slideIndex ,因此不应将其作为函数参数传递,因为那样会创建具有相同名称的局部变量,但是对其进行任何重新分配都不会影响全局变量。

 let slides_array = document.getElementsByClassName("mySlides"); let slideIndex = 0; showDivs(slideIndex); function plusDivs(n) { slideIndex += n; showDivs(); /// <---- } function showDivs() { /// <---- let i; if (slideIndex > (slides_array.length - 1)) { slideIndex = 0; }; if (slideIndex < 0) { slideIndex = (slides_array.length - 1); }; for (i=0; i<(slides_array.length); i++) { slides_array[i].style.display = "none"; } slides_array[slideIndex].style.display = "block"; } 
 <section class = "Slideshow"> <div id="slide1" class="mySlides"> <img src="http://cdn3-www.dogtime.com/assets/uploads/gallery/goldador-dog-breed-pictures/puppy-1.jpg"> </div> <div id="slide2" class="mySlides"> <img src="https://www.cesarsway.com/sites/newcesarsway/files/styles/large_article_preview/public/Shake-shiver-and-tremble-Why-dogs-do-it.jpg?itok=yvOUgQeL"> </div> <div id="slide3" class="mySlides"> <img src="https://i.kinja-img.com/gawker-media/image/upload/s--kx3d-Wgg--/c_scale,fl_progressive,q_80,w_800/ol9ceoqxidudap8owlwn.jpg"> </div> <div id="slide4" class="mySlides"> <img src="http://cdn1-www.dogtime.com/assets/uploads/gallery/korean-jindo-dog-breed-pictures/puppy-1_680-453.jpg"> </div> </section> <!-- Slideshow Buttons --> <button class="button" onclick="plusDivs(-1)"> &#10094; </button> <button class="button" onclick="plusDivs(+1)"> &#10095; </button> 

Alternatively, you could pass it as argument, but then make sure you do the modification to the global variable (outside of the function to which you pass the argument). 另外,您可以将其作为参数传递,但是请确保对全局变量(在传递参数的函数之外)进行了修改。

The scope problem mentioned by Duj and Trincot is the key problem here. Duj和Trincot提到的范围问题是这里的关键问题。 Moving the reset of slideIndex out of showDivs solves that. slideIndex的重置移出showDivs解决此问题。

A way of creating more self explanatory code is to change plusDivs into two functions, one which increments ( plusDivs ), and one which decrements ( minusDivs ). 创建更多自我说明性代码的一种方法是将plusDivs更改为两个函数,一个函数递增( plusDivs ),另一个递减( minusDivs )。 You can then update slideIndex from within those. 然后,您可以从其中更新slideIndex (These two functions use ternary operators to update slideIndex , which are pretty cool, and simple to implement). (这两个函数使用三元运算符来更新slideIndex ,它们非常酷,并且易于实现)。

let slides_array = document.getElementsByClassName("mySlides");

let slideIndex = 0;
showDivs(slideIndex);

function plusDivs() {
  slideIndex = slideIndex === slides_array.length - 1 ? 0 : slideIndex + 1;
  showDivs(slideIndex);
}

function minusDivs() {
  slideIndex = slideIndex === 0 ? slides_array.length - 1 : slideIndex - 1;
  showDivs(slideIndex);
}

function showDivs(slideIndex) {
  for (i = 0; i < slides_array.length; i++) {
    slides_array[i].style.display = "none";
  }
  slides_array[slideIndex].style.display = "block";
}

You can then update the HTML like this: 然后,您可以像这样更新HTML:

<!-- Slideshow Buttons -->
<button class="button" onclick="minusDivs()"> &#10094; </button>
<button class="button" onclick="plusDivs()"> &#10095; </button>

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

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