JavaScript carousel using array and for-loop: Why does slideIndex get "stuck" after first loop? -


i'm trying create javascript carousel of scrolling images without jquery, using array , for-loop.

at end of 4-object array of images, "next button" returns array[slideindex=0].

similarly, when hit "previous button" on first object in array, returns last object in array.

this function seems almost work code have right now. however, works once. after skipping backwards or forwards last or first object in array, doesn't continue scroll in intended direction.

can please tell me why slideindex doesn't continue loop plusdivs() function after first skip?

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);       };             (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 correct technical terms -- i'm trying describe current issue best can, maybe isn't best way describe problem. please advise.

the scope problem mentioned duj , trincot key problem here. moving reset of slideindex out of showdivs solves that.

a way of creating more self explanatory code change plusdivs 2 functions, 1 increments (plusdivs), , 1 decrements (minusdivs). can update slideindex within those. (these 2 functions use ternary operators update slideindex, pretty cool, , simple implement).

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) {   (i = 0; < slides_array.length; i++) {     slides_array[i].style.display = "none";   }   slides_array[slideindex].style.display = "block"; } 

you can update html this:

<!-- slideshow buttons --> <button class="button" onclick="minusdivs()"> &#10094; </button> <button class="button" onclick="plusdivs()"> &#10095; </button> 

Comments

Popular posts from this blog

resizing Telegram inline keyboard -

command line - How can a Python program background itself? -

php - "cURL error 28: Resolving timed out" on Wordpress on Azure App Service on Linux -