“上一页”和“下一页”按钮在网页中不起作用

时间:2018-01-06 09:57:05

标签: javascript html css

我创建了图片幻灯片。代码在

之下

HTML

strdup()

CSS

<div class="slideshow-container">

<div class="mySlides fade">
  <img src="https://i.pinimg.com/originals/bf/5f/95/bf5f9539ea9fa3eee60e961b7e50c8e1.jpg" style="width:100%">
</div>

<div class="mySlides fade">

  <img src="http://www.holifestival.org/images/holi-image-4-big.jpg" style="width:100%">

</div>

<div class="mySlides fade">

  <img src="https://searchengineland.com/figz/wp-content/seloads/2016/03/google-photos-images-camera-ss-1920-800x450.jpg" style="width:100%">
</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>

的JavaScript

* {box-sizing: border-box}
body {font-family: Verdana, sans-serif; margin:0}
.mySlides {display: none}
img {vertical-align: middle;}

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

/* Next & previous buttons */
.prev, .next {
  cursor: pointer;
  position: absolute;
  top: 50%;
  width: auto;
  padding: 16px;
  margin-top: -22px;
  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}
}

/* On smaller screens, decrease text size */
@media only screen and (max-width: 300px) {
  .prev, .next,.text {font-size: 11px}
}

它可以根据我的需要工作,但是当涉及到上一个按钮并且单击下一个按钮时,它不会按照上一张幻灯片或下一张幻灯片移动。

现场演示: https://codepen.io/RamBM/pen/ppdRGo

任何人都可以让我知道或纠正我在哪里出错。

1 个答案:

答案 0 :(得分:1)

这里的主要问题是你开始重命名但没有全部重命名(plusDivs / plusSlides,showDivs / showSlides)。

此外,slideIndex被声明两次,你应该删除第二个。

您也可能希望在手动导航时清除超时。 在开头添加var timeout,将setTimeout更改为timeout = setTimeout,然后更新plusSlides

function plusSlides(n) {
  if (timeout) {
    clearTimeout(timeout);
    timeout = null;
  }
  showSlides(slideIndex += n);
}

修改:示例更正后的代码集https://codepen.io/kLabz/pen/BJmWab?editors=1010