滑块不显示所有图像

时间:2018-06-03 13:59:25

标签: javascript html css slideshow uislider

我不知道为什么我的滑块按钮不起作用而且它们也没有显示所有图像....有些人请帮忙

                            <div class="slides">
 <img src="../page_images/1.jpeg" width="100%" height="100%"/>
                            </div>    
                            <div class="slides">
 <img src="../page_images/2.jpeg" width="100%" height="100%"/>
                            </div>
                            <div class="slides">
 <img src="../page_images/3.jpeg" width="100%" height="100%"/>
                            </div>
                     <div class="slides">
  <img src="../page_images/4.jpeg" width="100%" height="100%"/>
                                </div>

&安培;#10094 &安培;#10095

和java脚本

        var index = 1;

        function plusIndex(n){
            index = index + n;
            showImage(index);
        }


        showImage(index);

        function showImage(n){
            var i;
            var x = document.getElementsByClassName("slides");
            if(n > x.length){
                index = 1;
            }
            if(n < x.length){
                index = x.length;
            }
            for(i=0; i<x.length;i++){
                x[i].style.display = "none";
            }
            x[index-1].style.display = "block";
        }

1 个答案:

答案 0 :(得分:2)

此处的解决方案 -

您只需要更改n < x.length条件

&#13;
&#13;
var index = 1;

function plusIndex(n) {
  index = index + n;
  showImage(index);
}


showImage(index);

function showImage(n) {
  var i;
  var x = document.getElementsByClassName("slides");
  if (n > x.length) {
    index = 1;
  }
  if (n <= 0) {
    index = x.length;
  }
  for (i = 0; i < x.length; i++) {
    x[i].style.display = "none";
  }
  x[index - 1].style.display = "block";
}
&#13;
<div class="marbletypeleft">




  <div class="slides">
    <img src="../page_images/1.jpeg" alt="1" width="100%" height="100%" />
  </div>
  <div class="slides">
    <img src="../page_images/2.jpeg" alt="2" width="100%" height="100%" />
  </div>
  <div class="slides">
    <img src="../page_images/3.jpeg" alt="3" width="100%" height="100%" />
  </div>
  <div class="slides">
    <img src="../page_images/4.jpeg" alt="4" width="100%" height="100%" />
  </div>

  <button class="btn" onclick="plusIndex(-1)" id="btn1">&#10094</button>
  <button class="btn" onclick="plusIndex(1)" id="btn2">&#10095</button>
</div>
&#13;
&#13;
&#13;