简体   繁体   中英

Image slider jquery not working

 $("#forward_carousel").click( () => { //sets timeout back and starts clearTimeout(timeout); timeout = 0; var i; var x = $(".carousel_size");//class for the pictures in carousel for (i = 0; i < x.length; i++) { x[i].style.display = "none"; } slideIndex++; if (slideIndex > x.length) {slideIndex = 1} x[slideIndex-1].style.display = "block"; timeout =setInterval(carousel, 5000); }) $("#backward_carousel").click( () => { clearTimeout(timeout); timeout = 0; var i; var x = $(".carousel_size");//class for the pictures in carousel for (i = 0; i < x.length; i++) { x[i].style.display = "none"; } slideIndex--; if (slideIndex < x.length){slideIndex = 3} x[slideIndex-1].style.display = "block"; timeout =setInterval(carousel, 5000); }) 
the forward button is working, but the backward button is just working if the current picture is at index 1. If the index is not at 1 the backward button is not working at all.

Your condition for backward index is not correct.

Change this line:

 if (slideIndex < x.length){slideIndex = 3}

To this:

 if (slideIndex < 0){slideIndex = x.length}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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