Search code examples
javascripthtmlslideshow

How do I keep a slideshow from looping back to the beginning?


I am currently trying to make a slideshow in JavaScript based on the sample code provided on W3Schools.com. However, with this sample slideshow, when the next button is pressed on the last page, it goes back to page one. Similarly, when the previous button is pressed on the first page, it goes to the last page. I want to have the slideshow not loop around in either direction, so the previous button doesn't work or doesn't exist on the first page and the next button doesn't work or doesn't exist on the first page. How would I do that?

(The code is just the sample code provided from W3Schools.com, because that's what I'm working off of.)

<body>

<div class="slideshow-container">

<div class="mySlides fade">
  <div class="numbertext">1 / 3</div>
  <img src="img_nature_wide.jpg" style="width:100%">
  <div class="text">Caption Text</div>
</div>

<div class="mySlides fade">
  <div class="numbertext">2 / 3</div>
  <img src="img_fjords_wide.jpg" style="width:100%">
  <div class="text">Caption Two</div>
</div>

<div class="mySlides fade">
  <div class="numbertext">3 / 3</div>
  <img src="img_mountains_wide.jpg" style="width:100%">
  <div class="text">Caption Three</div>
</div>

<a class="prev" onclick="plusSlides(-1)">&#10094;</a>
<a class="next" onclick="plusSlides(1)">&#10095;</a>

</div>
<br>

<div style="text-align:center">
  <span class="dot" onclick="currentSlide(1)"></span> 
  <span class="dot" onclick="currentSlide(2)"></span> 
  <span class="dot" onclick="currentSlide(3)"></span> 
</div>

<script>
var slideIndex = 1;
showSlides(slideIndex);

function plusSlides(n) {
  showSlides(slideIndex += n);
}

function currentSlide(n) {
  showSlides(slideIndex = n);
}

function showSlides(n) {
  var i;
  var slides = document.getElementsByClassName("mySlides");
  var dots = document.getElementsByClassName("dot");
  if (n > slides.length) {slideIndex = 1}    
  if (n < 1) {slideIndex = slides.length}
  for (i = 0; i < slides.length; i++) {
      slides[i].style.display = "none";  
  }
  for (i = 0; i < dots.length; i++) {
      dots[i].className = dots[i].className.replace(" active", "");
  }
  slides[slideIndex-1].style.display = "block";  
  dots[slideIndex-1].className += " active";
}
</script>

</body>


Solution

  • Just remove the resetting code and leave if n < 1 or > than the slides

    //  if (n > slides.length) {slideIndex = 1} // code to check end    
    //  if (n < 1) {slideIndex = slides.length} // code to check beginning
    
    if (n>slides.length || n<1) return; // my addition to not execute if either
    

    var slideIndex = 1;
    showSlides(slideIndex);
    
    function plusSlides(n) {
      showSlides(slideIndex += n);
    }
    
    function currentSlide(n) {
      showSlides(slideIndex = n);
    }
    
    function showSlides(n) {
      var i, slides = document.getElementsByClassName("mySlides");
    
      if (n > slides.length || n < 1) return;
      var dots = document.getElementsByClassName("dot");
      //  if (n > slides.length) {slideIndex = 1}    
      //  if (n < 1) {slideIndex = slides.length}
      for (i = 0; i < slides.length; i++) {
        slides[i].style.display = "none";
      }
      for (i = 0; i < dots.length; i++) {
        dots[i].className = dots[i].className.replace(" active", "");
      }
      slides[slideIndex - 1].style.display = "block";
      dots[slideIndex - 1].className += " active";
    }
    <body>
    
      <div class="slideshow-container">
    
        <div class="mySlides fade">
          <div class="numbertext">1 / 3</div>
          <img src="img_nature_wide.jpg" style="width:100%">
          <div class="text">Caption Text</div>
        </div>
    
        <div class="mySlides fade">
          <div class="numbertext">2 / 3</div>
          <img src="img_fjords_wide.jpg" style="width:100%">
          <div class="text">Caption Two</div>
        </div>
    
        <div class="mySlides fade">
          <div class="numbertext">3 / 3</div>
          <img src="img_mountains_wide.jpg" style="width:100%">
          <div class="text">Caption Three</div>
        </div>
    
        <a class="prev" onclick="plusSlides(-1)">&#10094;</a>
        <a class="next" onclick="plusSlides(1)">&#10095;</a>
    
      </div>
      <br>
    
      <div style="text-align:center">
        <span class="dot" onclick="currentSlide(1)"></span>
        <span class="dot" onclick="currentSlide(2)"></span>
        <span class="dot" onclick="currentSlide(3)"></span>
      </div>