为什么幻灯片放映会跳过最后一张幻灯片?

时间:2019-02-06 21:10:17

标签: javascript html css

我使用JavaScript,CSS和HTML制作了幻灯片。代码跳过了最后一张幻灯片,我不明白为什么。因此,如果有人可以启发为什么这样做,那就太好了。

我尝试更改JavaScript中的内容,例如添加到幻灯片中或知道我需要它而取消了-1,但没有任何效果,我很沮丧。

window.onload = function() { // add window.onload here and set it euqal to a function
  var timeOut;
  var slideIndex = 0;
  var slides;
  showSlides(slideIndex);
  // Next/previous controls
  window.plusSlides = function(n) {
    showSlides(n);
  }
  // Thumbnail image controls
  window.currentSlide = function(n) {
    slideIndex = (n - 1)
    showSlides();
  }

  function bind() {
    clearInterval(timeOut)
    timeOut = setInterval(showSlides, 5000);
  }

  function showSlides(n) {
    var i;
    slides = document.getElementsByClassName("mySlides");
    var dots = document.getElementsByClassName("dot");

    for (i = 0; i < slides.length; i++) {
      slides[i].style.display = "none";
    }
    if (n)
      slideIndex += n;
    else slideIndex++;
    if (slideIndex >= slides.length || slideIndex < 1) {
      slideIndex = 1
    }

    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";
    bind();
  }

}
* {
  box-sizing: border-box
}


/* Slideshow container */

.slideshow-container {
  max-width: 1000px;
  position: relative;
  margin: auto;
}


/* Hide the images by default */

.mySlides {
  display: none;
}


/* Next & previous buttons */

.prev,
.next {
  cursor: pointer;
  position: absolute;
  top: 50%;
  width: auto;
  margin-top: -22px;
  padding: 16px;
  color: white;
  font-weight: bold;
  font-size: 18px;
  transition: 0.6s ease;
  border-radius: 0 3px 3px 0;
  user-select: none;
}


/* Position the "next button" to the right */

.next {
  right: -15px;
  border-radius: 3px 0 0 3px;
}


/* On hover, add a black background color with a little bit see-through 
.prev:hover, .next:hover {
  background-color: rgba(0,0,0,0.8);
}
*/


/* Caption text */

.text {
  color: #f2f2f2;
  font-size: 28px;
  padding: 8px 12px;
  position: absolute;
  bottom: 8px;
  width: 100%;
  text-align: center;
}


/* Number text (1/3 etc) */

.numbertext {
  color: #f2f2f2;
  font-size: 12px;
  padding: 8px 12px;
  position: absolute;
  top: 0;
}


/* The dots/bullets/indicators */

.dot {
  cursor: pointer;
  height: 15px;
  width: 15px;
  margin: 0 2px;
  background-color: #bbb;
  border-radius: 50%;
  display: inline-block;
  transition: background-color 0.6s ease;
}

.active,
.dot:hover {
  background-color: #717171;
}


/* Fading animation */

.fade {
  -webkit-animation-name: fade;
  -webkit-animation-duration: 1.5s;
  animation-name: fade;
  animation-duration: 1.5s;
}

@-webkit-keyframes fade {
  from {
    opacity: .4
  }
  to {
    opacity: 1
  }
}

@keyframes fade {
  from {
    opacity: .4
  }
  to {
    opacity: 1
  }
}
<div class="slideshow-container">

  <!-- Full-width images with number and caption text -->
  <div class="mySlides fade">
    <img src="https://upload.wikimedia.org/wikipedia/commons/e/e1/Nuernberg-fronfeste-und-kettensteg-v-O.jpg" style="width:1000px; height: 540px;">
    <div class="text"><span style="background-color: black">one on one weekly siminars</span></div>
  </div>

  <div class="mySlides fade">
    <img src="https://upload.wikimedia.org/wikipedia/commons/e/e1/Nuernberg-fronfeste-und-kettensteg-v-O.jpg" style="width:1000px; height: 540px;">
    <div class="text"><span style="background-color: black">monthly seminars/workshops</span></div>
  </div>

  <div class="mySlides fade">
    <img src="https://upload.wikimedia.org/wikipedia/commons/e/e1/Nuernberg-fronfeste-und-kettensteg-v-O.jpg" style="width:1000px; height: 540px;">
    <div class="text"><span style="background-color: black">nursing home visits</span></div>
  </div>

  <div class="mySlides fade">
    <img src="https://upload.wikimedia.org/wikipedia/commons/e/e1/Nuernberg-fronfeste-und-kettensteg-v-O.jpg" style="width:1000px; height: 540px;">
    <div class="text"><span style="background-color: black">community service</span></div>
  </div>

  <div class="mySlides fade">
    <img src="https://upload.wikimedia.org/wikipedia/commons/e/e1/Nuernberg-fronfeste-und-kettensteg-v-O.jpg" style="width:1000px; height: 540px;">
    <div class="text"><span style="background-color: black">college visits</span></div>
  </div>

  <div class="mySlides fade">
    <img src="https://upload.wikimedia.org/wikipedia/commons/e/e1/Nuernberg-fronfeste-und-kettensteg-v-O.jpg" style="width:1000px; height: 540px;">
    <div class="text"><span style="background-color: black">Annual Summer Retreat</span></div>
  </div>

  <div class="mySlides fade">
    <img src="pictures%20updated/12.jpg" style="width:1000px; height: 540px;">
    <div class="text"><span style="background-color: black">Annual Horse Camp</span></div>
  </div>

  <div class="mySlides fade">
    <img src="https://upload.wikimedia.org/wikipedia/commons/e/e1/Nuernberg-fronfeste-und-kettensteg-v-O.jpg" style="width:1000px; height: 540px;">
    <div class="text"><span style="background-color: black">Annual Horse Camp</span></div>
  </div>


  <!-- Next and previous buttons -->
  <a class="prev" onclick="plusSlides(-1)">&#10094;</a>
  <a class="next" onclick="plusSlides(1)">&#10095;</a>
</div>
<br>



<!-- The dots/circles -->
<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>
  <span class="dot" onclick="curretnslide(4)"></span>
  <span class="dot" onclick="currentslide(5)"></span>
  <span class="dot" onclick="currentslide(6)"></span>
  <span class="dot" onclick="currentslide(7)"></span>
  <span class="dot" onclick="currentslide(8)"></span>
</div>
</body>

1 个答案:

答案 0 :(得分:0)

您的函数showSlides()检查slideIndex是否大于或等于幻灯片的长度。然后,您使用slideIndex - 1访问幻灯片元素。在slideIndex == slides.length的情况下,即使您将数组括号内的偏移量为1(从1算起)也要重置为1。

以下更正的if语句应该起作用:

if (slideIndex > slides.length || slideIndex < 1) {
    slideIndex = 1
}

我建议您避免在数组括号内执行算术运算。当您这样做时,它会破坏名称“ slideIndex”的含义,因为它实际上并不是您正在使用的幻灯片索引。