我的幻灯片代码中的上一个按钮不起作用

时间:2018-10-16 18:48:15

标签: javascript

我正在通过混合w3school示例的两个代码来创建幻灯片代码,所有其他功能(如“下一步”按钮)都可以正常工作。只有上一个按钮(用于加载上一个图像)不起作用。我正在调试JavaScript,但无法找到或纠正该错误。

SlideShow ScreenShot

/slideshow.html

var slideIndex = 0;
showSlides(slideIndex);

function plusSlides(n) {
    clearTimeout(myTime);
    if(n===1){showSlides(slideIndex += (n-1));}
    if(n===-1){showSlides(slideIndex += n);}
    clearTimeout(myTime);
}

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

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

    for (i = 0; i < slides.length; i++) {
       slides[i].style.display = "none";  
    }
    slideIndex++;
    if (slideIndex > slides.length) {slideIndex = 1}
    if (num > slides.length) {slideIndex = 0}    
    if (num < 1) {slideIndex = slides.length - 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";
    myTime = setTimeout(showSlides, 3000); // Change image every 3 seconds
}

function start_timeout() {
    myTime = setTimeout(showSlides, 3000); // Change image every 3 seconds
}

function stop_timeout() {
    clearTimeout(myTime);
}

/slideshow_script.js

* {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;
}

/* Position the "next button" to the right */
.next {
  right: 0;
  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: 15px;
  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}
}

/slideshow_style.css

application.Session

我知道这是一个愚蠢的问题,但我被困住了。请帮帮我,我如何使此代码正常工作。请指出错误。预先感谢。

1 个答案:

答案 0 :(得分:0)

尝试替换行

if(n===-1){showSlides(slideIndex += n);}

使用

if(n===-1){showSlides(slideIndex += (n-1));}

另外,在两行之前

if (num > slides.length) {slideIndex = 0}    
if (num < 1) {slideIndex = slides.length - 1}

添加以下行:

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

还删除或注释掉if (num ...)开头的两行。


说实话,您的代码有些方面令人困惑,并且没有帮助您。在我看来,您似乎还没有完全理解所编写的代码,而只是将它们放进去,因为它们似乎使它可以工作。

一旦您的代码正常工作,就有很多范围可以更好地编写它。看看https://codereview.stackexchange.com/