开发者问题收集

如何在一页上获取多个滑块?

2019-01-08
10728

我试图创建一个包含多个滑块的单页网站。但是滑块超过 2 个之后,它就不再工作了。如何修复它?

我从 w3schools 获得了多个滑块的代码(尝试使用 w3.css 框架构建网站)。 https://www.w3schools.com/howto/howto_js_slideshow.asp

我尝试将滑块从 2 个复制到 6 个,并将“var slideId = ["mySlides1", "mySlides2"] ”更改为我定义的 6 个。但是它不起作用。

<!DOCTYPE html>
<html lang="de">
    <head>
        <title>Multiple Slider</title>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <link href="css/w3.css" rel="stylesheet">
        <link href="css/style.css" rel="stylesheet">
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
        <script src="js/side-menu.js"></script>
    </head>

    <body>

<div class="w3-content w3-display-container">
  <img class="mySlides1" src="img_snowtops.jpg" style="width:100%">
  <img class="mySlides1" src="img_lights.jpg" style="width:100%">
  <img class="mySlides1" src="img_mountains.jpg" style="width:100%">
  <img class="mySlides1" src="img_forest.jpg" style="width:100%">
  <button class="w3-button w3-black w3-display-left" onclick="plusDivs(-1, 0)">&#10094;</button>
  <button class="w3-button w3-black w3-display-right" onclick="plusDivs(1, 0)">&#10095;</button>
</div>

<div class="w3-content w3-display-container">
  <img class="mySlides2" src="img_snowtops.jpg" style="width:100%">
  <img class="mySlides2" src="img_lights.jpg" style="width:100%">
  <img class="mySlides2" src="img_mountains.jpg" style="width:100%">
  <img class="mySlides2" src="img_forest.jpg" style="width:100%">
  <button class="w3-button w3-black w3-display-left" onclick="plusDivs(-1, 0)">&#10094;</button>
  <button class="w3-button w3-black w3-display-right" onclick="plusDivs(1, 0)">&#10095;</button>
</div>

<div class="w3-content w3-display-container">
  <img class="mySlides3" src="img_snowtops.jpg" style="width:100%">
  <img class="mySlides3" src="img_lights.jpg" style="width:100%">
  <img class="mySlides3" src="img_mountains.jpg" style="width:100%">
  <img class="mySlides3" src="img_forest.jpg" style="width:100%">
  <button class="w3-button w3-black w3-display-left" onclick="plusDivs(-1, 0)">&#10094;</button>
  <button class="w3-button w3-black w3-display-right" onclick="plusDivs(1, 0)">&#10095;</button>
</div>

<div class="w3-content w3-display-container">
  <img class="mySlides4" src="img_snowtops.jpg" style="width:100%">
  <img class="mySlides4" src="img_lights.jpg" style="width:100%">
  <img class="mySlides4" src="img_mountains.jpg" style="width:100%">
  <img class="mySlides4" src="img_forest.jpg" style="width:100%">
  <button class="w3-button w3-black w3-display-left" onclick="plusDivs(-1, 0)">&#10094;</button>
  <button class="w3-button w3-black w3-display-right" onclick="plusDivs(1, 0)">&#10095;</button>
</div>

<div class="w3-content w3-display-container">
  <img class="mySlides5" src="img_snowtops.jpg" style="width:100%">
  <img class="mySlides5" src="img_lights.jpg" style="width:100%">
  <img class="mySlides5" src="img_mountains.jpg" style="width:100%">
  <img class="mySlides5" src="img_forest.jpg" style="width:100%">
  <button class="w3-button w3-black w3-display-left" onclick="plusDivs(-1, 0)">&#10094;</button>
  <button class="w3-button w3-black w3-display-right" onclick="plusDivs(1, 0)">&#10095;</button>
</div>

<div class="w3-content w3-display-container">
  <img class="mySlides6" src="img_snowtops.jpg" style="width:100%">
  <img class="mySlides6" src="img_lights.jpg" style="width:100%">
  <img class="mySlides6" src="img_mountains.jpg" style="width:100%">
  <img class="mySlides6" src="img_forest.jpg" style="width:100%">
  <button class="w3-button w3-black w3-display-left" onclick="plusDivs(-1, 0)">&#10094;</button>
  <button class="w3-button w3-black w3-display-right" onclick="plusDivs(1, 0)">&#10095;</button>
</div>

<script>
var slideIndex = [1,1];
var slideId = ["mySlides1","mySlides2","mySlides3","mySlides4","mySlides5","mySlides6"]
showDivs(1, 0);
showDivs(1, 1);

function plusDivs(n, no) {
  showDivs(slideIndex[no] += n, no);
}

function showDivs(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>

我期望输出可以正常工作,但不知道为什么它不起作用。

2个回答

您在每个滑块中传递了错误的索引,

plusDivs(-1, 0) //this is for first

您对所有其他滑块都执行了相同的操作,而您应该这样做。

plusDivs(-1, 0) //first

plusDivs(-1, 1) //second

plusDivs(-1, 2) //third

这样您的滑块就可以正常工作了。

var slideIndex = [1, 1];
var slideId = ["mySlides1", "mySlides2"]
showDivs(1, 0);
showDivs(1, 1);

function plusDivs(n, no) {
  showDivs(slideIndex[no] += n, no);
}

function showDivs(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";
}
<!DOCTYPE html>
<html lang="de">

<head>
  <title>Multiple Slider</title>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link href="css/w3.css" rel="stylesheet">
  <link href="css/style.css" rel="stylesheet">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
  <script src="js/side-menu.js"></script>
</head>

<body>

  <div class="w3-content w3-display-container">
    <img class="mySlides1" src="https://assets.pernod-ricard.com/nz/media_images/test.jpg?hUV74FvXQrWUBk1P2.fBvzoBUmjZ1wct" style="width:100%">
    <img class="mySlides1" src="https://images.sftcdn.net/images/t_app-logo-l,f_auto,dpr_auto/p/ce2ece60-9b32-11e6-95ab-00163ed833e7/2183169552/the-test-fun-for-friends-logo.png" style="width:100%">
    <button class="w3-button w3-black w3-display-left" onclick="plusDivs(-1, 0)">&#10094;</button>
  <button class="w3-button w3-black w3-display-right" onclick="plusDivs(1, 0)">&#10095;</button>
  
  </div>

  <div class="w3-content w3-display-container">
    <img class="mySlides2" src="https://assets.pernod-ricard.com/nz/media_images/test.jpg?hUV74FvXQrWUBk1P2.fBvzoBUmjZ1wct" style="width:100%">
    <img class="mySlides2" src="https://images.sftcdn.net/images/t_app-logo-l,f_auto,dpr_auto/p/ce2ece60-9b32-11e6-95ab-00163ed833e7/2183169552/the-test-fun-for-friends-logo.png" style="width:100%">
    <button class="w3-button w3-black w3-display-left" onclick="plusDivs(-1, 1)">&#10094;</button>
  <button class="w3-button w3-black w3-display-right" onclick="plusDivs(1, 1)">&#10095;</button>
  
  </div>





</body>

</html>
Just code
2019-01-08

多幻灯片 html 内容

<h2 style="text-align:center">Multiple Slideshows</h2>

<p>Slideshow 1:</p>
<div class="slideshow-container">
  <div class="mySlides1">
    <img src="q1.jpeg" style="width:100%">
  </div>

  <div class="mySlides1">
    <img src="q2.jpeg" style="width:100%">
  </div>

  <div class="mySlides1">
    <img src="q3.jpeg" style="width:100%">
  </div>

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

<p>Slideshow 2:</p>
<div class="slideshow-container">
  <div class="mySlides2">
    <img src="q3.jpeg" style="width:100%">
  </div>

  <div class="mySlides2">
    <img src="q2.jpeg" style="width:100%">
  </div>

  <div class="mySlides2">
    <img src="q1.jpeg" style="width:100%">
  </div>

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


<p>Slideshow 3:</p>
<div class="slideshow-container">
  <div class="mySlides3">
    <img src="q2.jpeg" style="width:100%">
  </div>

  <div class="mySlides3">
    <img src="q1.jpeg" style="width:100%">
  </div>

  <div class="mySlides3">
    <img src="q3.jpeg" style="width:100%">
  </div>

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


<p>Slideshow 4:</p>
<div class="slideshow-container">
  <div class="mySlides4">
    <img src="q1.jpeg" style="width:100%">
  </div>

  <div class="mySlides4">
    <img src="q2.jpeg" style="width:100%">
  </div>



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

脚本根据多幻灯片索引进行更改

<script>
var slideIndex = [1,1,1,1,1]; //.. multi slide index count

var slideId = ["mySlides1", "mySlides2","mySlides3","mySlides4","mySlides5"] // .. multi slide class  name in array


//initalization 
showSlides(1, 0);  // zero  index
showSlides(1, 1);  // first  
showSlides(1, 2);  // second  
showSlides(1, 3);  // third 
showSlides(1, 4);   //four 
//....

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


</script>

尝试此代码。

Murugesan Rathinam
2019-01-08