简体   繁体   中英

JavaScript slideshow displaying all images

My slide show displays all images at once in a row until clicking the next or previous buttons then the slideshow functions properly. I have found a similar thread and the problem ended up being a syntax error. I have read the code over and over and I cannot see the problem. Please let me know what I am doing wrong. Thank you!

    .ssbutton{
        border:none;
        display:inline-block;
        outline:0;
        padding:8px 16px;
        vertical-align:middle;
        overflow:hidden;
        text-decoration:none;
        color:inherit;
        background-color:inherit;
        text-align:center;
        cursor:pointer;
        white-space:nowrap
    }



    var slideIndex = 1;

    showDivs(slideIndex);

    function plusDivs(n) {
      showDivs(slideIndex += n);
    }

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





<div class="slideShow">
     <img class="Slide" src="images/Pic1.png">
     <img class="Slide" src="images/Pic2.png">
     <img class="Slide" src="images/Pic3.png">
     <img class="Slide" src="images/Pic4.png">
        <button class="ssbutton" onclick="plusDivs(-1)">&#10094;Prev</button>
        <button class="ssbutton" onclick="plusDivs(1)">Next &#10095;</button>
</div>

You need the .Slide elements to be hidden by default, you can set that up in the CSS:

.Slide {
   display: none;
}
/* show first slide by default */
.Slide:first-child {
   display: block;
}

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