图像滑块jquery无法正常工作

时间:2018-03-03 12:04:48

标签: javascript jquery

$("#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);
})
前进按钮正在工作,但如果当前图片位于索引1,则后退按钮才起作用。如果索引不是1,则后退按钮根本不起作用。

1 个答案:

答案 0 :(得分:0)

您的落后索引条件不正确。

更改此行:

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

对此:

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