如何添加第三个&第四张幻灯片到此代码(并保持其功能)?

时间:2018-04-01 00:51:52

标签: javascript slideshow

我正在尝试在我正在处理的朋友的页面上使用w3schools的多幻灯片 - 只有两个幻灯片显示它可以正常工作但是当你尝试添加更多时,事情就会停止工作。第三个幻灯片按钮控制第二个幻灯片放映&第四个没有出现在页面上。我还添加了另一个w3幻灯片中的小导航点:goo.gl/2bFJk2,但它们也不起作用。

我尝试改变代码,但我对JS的了解非常有限,所以我只是在黑暗中拍摄。我在var slideID中添加了更多“myslides”,向var slideIndex添加了更多(1),并添加了更多“showslides”,尝试使用前两个模式(#,#)但到目前为止没有运气。 / p>

如果有人能帮我解决这个问题,我真的很感激。感谢

以下是w3幻灯片'试试'页面的网址:https://www.w3schools.com/howto/tryit.asp?filename=tryhow_js_slideshow_multiple

以下是代码:

<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
* {box-sizing: border-box}
body {font-family: Verdana, sans-serif; margin:0}
.mySlides1, .mySlides2, .mySlides3, .mySlides4, .mySlides5 {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;
  user-select: none;
}

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

/* On hover, add a grey background color */
.prev:hover, .next:hover {
  background-color: #f1f1f1;
  color: black;
}

/* 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}
}

/* 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;
}
</style>
</head>
<body>

<div class="slideshow-container">
  <div class="mySlides1 fade">
    <img src="http://i63.tinypic.com/ajy0x1.jpg" style="width:100%">
  </div>

  <div class="mySlides1 fade">
    <img src="http://i65.tinypic.com/2i71vv4.jpg" style="width:100%">
  </div>

  <div class="mySlides1 fade">
    <img src="http://i63.tinypic.com/6p6mhi.jpg" style="width:100%">
  </div>

  <a class="prev" onclick="plusSlides(-1, 0)">&#10094;</a>
  <a class="next" onclick="plusSlides(1, 0)">&#10095;</a>
</div>

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

<br>

<div class="slideshow-container">
  <div class="mySlides2 fade">
    <img src="http://i63.tinypic.com/ajy0x1.jpg" style="width:100%">
  </div>

  <div class="mySlides2 fade">
    <img src="http://i65.tinypic.com/2i71vv4.jpg" style="width:100%">
  </div>

  <div class="mySlides2 fade">
    <img src="http://i63.tinypic.com/6p6mhi.jpg" style="width:100%">
  </div>

  <a class="prev" onclick="plusSlides(-1, 1)">&#10094;</a>
  <a class="next" onclick="plusSlides(1, 1)">&#10095;</a>
</div>

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

<br>

<div class="slideshow-container">
  <div class="mySlides3 fade">
    <img src="http://i63.tinypic.com/ajy0x1.jpg" style="width:100%">
  </div>

  <div class="mySlides3 fade">
    <img src="http://i65.tinypic.com/2i71vv4.jpg" style="width:100%">
  </div>

  <div class="mySlides3 fade">
    <img src="http://i63.tinypic.com/6p6mhi.jpg" style="width:100%">
  </div>

  <a class="prev" onclick="plusSlides(-1, 1)">&#10094;</a>
  <a class="next" onclick="plusSlides(1, 1)">&#10095;</a>
</div>

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

<br>

<div class="slideshow-container">
  <div class="mySlides4 fade">
    <img src="http://i63.tinypic.com/ajy0x1.jpg" style="width:100%">
  </div>

  <div class="mySlides4 fade">
    <img src="http://i65.tinypic.com/2i71vv4.jpg" style="width:100%">
  </div>

  <div class="mySlides4 fade">
    <img src="http://i63.tinypic.com/6p6mhi.jpg" style="width:100%">
  </div>

  <a class="prev" onclick="plusSlides(-1, 1)">&#10094;</a>
  <a class="next" onclick="plusSlides(1, 1)">&#10095;</a>
</div>

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

<br>

<div class="slideshow-container">
  <div class="mySlides5 fade">
    <img src="http://i63.tinypic.com/ajy0x1.jpg" style="width:100%">
  </div>

  <div class="mySlides5 fade">
    <img src="http://i65.tinypic.com/2i71vv4.jpg" style="width:100%">
  </div>

  <div class="mySlides5 fade">
    <img src="http://i63.tinypic.com/6p6mhi.jpg" style="width:100%">
  </div>

  <a class="prev" onclick="plusSlides(-1, 1)">&#10094;</a>
  <a class="next" onclick="plusSlides(1, 1)">&#10095;</a>
</div>

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

<br>


<script>
var slideIndex = [1,1,1,1,1];
var slideId = ["mySlides1", "mySlides2", "mySlides3", "mySlides4", "mySlides5"]
showSlides(1, 0);
showSlides(1, 1);
showSlides(2, 1);
showSlides(3, 1);
showSlides(4, 2);

function plusSlides(n, no) {
  showSlides(slideIndex[no] += n, no);
}

function showSlides(n, no) {
  var i;
  var x = document.getElementsByClassName(slideId[no]);
  if (n > x.length) {slideIndex[no] = 1}    
  if (n < 1) {slideIndex[no] = x.length}
  for (i = 0; i < x.length; i++) {
     x[i].style.display = "none";  
  }
  x[slideIndex[no]-1].style.display = "block";  
}

</script>

</body>
</html> 

0 个答案:

没有答案