重复下一个&上一个按钮功能

时间:2016-10-05 08:25:45

标签: javascript html css

我不知道我需要做什么来重复删除第一个幻灯片容器按钮。当我点击第一个幻灯片容器上的下一个和上一个按钮时,这个按钮没有改变,只有secound幻灯片容器改变了。
您只能在浏览器中运行更改图片路径。

的index.html



<!DOCTYPE html>
<html>
<meta name="viewport" content="width=device-width, initial-scale=1">

<style>
* {box-sizing:border-box}
body {font-family: Verdana,sans-serif;margin:0}
.mySlides1 {display:none}

body {
    background: #2c3338;
}

/* Slideshow container */
.slideshow-container1 {
  max-width: 350px;
  position: relative;
  top: 20px;
  margin: auto;
}

/* Next & previous buttons */
.prev1, .next1 {
  cursor: pointer;
  position: absolute;
  top: 50%;
  width: auto;
  padding: 16px;
  margin-top: -22px;
  color: green;
  font-weight: bold;
  font-size: 18px;
  transition: 0.6s ease;
  border-radius: 0 3px 3px 0;
}

/* Position the "next button" to the right */
.next1 {
  right: 0;
  border-radius: 3px 0 0 3px;
}

/* On hover, add a black background color with a little bit see-through */
.prev1:hover, .next1:hover {
  background:#696969;
}

/* 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) {
  .prev1, .next1,.text {font-size: 11px}
}
</style>

<style>
* {box-sizing:border-box}
body {font-family: Verdana,sans-serif;margin:0}
.mySlides2 {display:none}

/* Slideshow container */
.slideshow-container2 {
  max-width: 350px;
  position: relative;
  top: 150px;
  margin: auto;
}

/* Next & previous buttons */
.prev2, .next2 {
  cursor: pointer;
  position: absolute;
  top: 50%;
  width: auto;
  padding: 16px;
  margin-top: -22px;
  color: red;
  font-weight: bold;
  font-size: 18px;
  transition: 0.6s ease;
  border-radius: 0 3px 3px 0;
}

/* Position the "next button" to the right */
.next2 {
  right: 0;
  border-radius: 3px 0 0 3px;
}

/* On hover, add a black background color with a little bit see-through */
.prev2:hover, .next2:hover {
  background:#696969;
}

/* 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) {
  .prev2, .next2,.text {font-size: 11px}
}
</style>
<body>

<div class="slideshow-container1">

<div class="mySlides1 fade">
  <img src="img/thumb-1.jpg" style="width:100%">
</div>

<div class="mySlides1 fade">
  <img src="img/thumb-2.jpg" style="width:100%">
</div>

<div class="mySlides1 fade">
  <img src="img/thumb-3.jpg" style="width:100%">
</div>

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

</div>
<br>

<div style="text-align:center">
  <span class="dot1" onclick="currentSlide(1)"></span>
  <span class="dot1" onclick="currentSlide(2)"></span>
  <span class="dot1" 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("mySlides1");
  var dots = document.getElementsByClassName("dot1");
  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>

<body>

<div class="slideshow-container2">

<div class="mySlides2 fade">
  <img src="img/thumb-1.jpg" style="width:100%">
</div>

<div class="mySlides2 fade">
  <img src="img/thumb-2.jpg" style="width:100%">
</div>

<div class="mySlides2 fade">
  <img src="img/thumb-3.jpg" style="width:100%">
</div>

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

</div>
<br>

<div style="text-align:center">
  <span class="dot2" onclick="currentSlide(1)"></span>
  <span class="dot2" onclick="currentSlide(2)"></span>
  <span class="dot2" 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("mySlides2");
  var dots = document.getElementsByClassName("dot2");
  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>
</html>
&#13;
&#13;
&#13;

提前致谢!

2 个答案:

答案 0 :(得分:0)

你的JS函数和变量是重复的(它总会以一种奇怪的方式作出反应)。您应该更改它们的名称或重构它们以对两个幻灯片显示使用相同的功能。

答案 1 :(得分:0)

在您的情况下,您可以更改第二个脚本的所有函数和变量名称:

slideIndex->slideIndex2
plusSlides -> plusSlides2
currentSlide -> currentSlide2
showSlides -> showSlides2

但您可以尝试不通过将变量传递给函数来复制脚本:

plusSlides(1, 1) // mean next picture in slide 1
plusSlides(1, 1) // mean next picture in slide 1
plusSlides(2, -1) // mean previous picture in slide 2
plusSlides(2, -1) // mean previous picture in slide 2