javascript - 为什么幻灯片会跳过最后一张幻灯片?

标签 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>

最佳答案

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

下面更正的 if 语句应该可以工作:

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

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

关于javascript - 为什么幻灯片会跳过最后一张幻灯片?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54562598/

相关文章:

javascript - 如何在chrome扩展程序的顶部显示消息?

javascript - 如何将所有像素大小的图像放入一个 div 中?

javascript - Fancybox 不工作(初学者)

html - svg clipPath 不剪辑

html - CSS 无法正常工作

javascript - 如何修复 webkit-fake-url 的意思?

javascript - jQuery `map` 将数组转换为 `dom` 元素

javascript - 在网格中绘制单元格

php - 我在自定义 wordpress 主题中做错了什么

html - 文本换行在 firefox 中有效,但在 IE 中无效