Having Trouble With Multiple Slideshows On One Page

Hey everyone,

I tried using a w3schools’ multiple-slideshow code for a page I’m working on. It works fine with only two of them, but when you add more, things start breaking down. The third slideshow buttons control the second slideshow & the fourth / fifth don’t even show up on the page. I also added the little dots from another w3 slideshow, but they don’t work either.

The original code was for 2 slideshows - I tried altering it a bit, but my knowledge of JS is pretty limited so I’m sort of just shooting in the dark here. I added more “myslides” to the var slideID and also added more “showslides”, trying to extrapolate the pattern of the first two (#, #) but no luck so far.

I’d like to have one of these slideshows for each subsection of a blog entry. If anyone could help me out with this I’d really appreciate it. Here’s the code (sorry, couldn’t add images as a new user here so I had to take out the image tags):

<!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">
    <!--Image here--> 
  </div>

  <div class="mySlides1 fade">
    <!--Image here--> 
  </div>

  <div class="mySlides1 fade">
     <!--Image here--> 
  </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">
    <!--Image here--> 
  </div>

  <div class="mySlides2 fade">
    <!--Image here--> 
  </div>

  <div class="mySlides2 fade">
    <!--Image here--> 
  </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">
    <!--Image here--> 
  </div>

  <div class="mySlides3 fade">
    <!--Image here--> 
  </div>

  <div class="mySlides3 fade">
    <!--Image here-->
  </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">
    <!--Image here--> 
  </div>

  <div class="mySlides4 fade">
    <!--Image here--> 
  </div>

  <div class="mySlides4 fade">
    <!--Image here--> 
  </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">
    <!--Image here-->
  </div>

  <div class="mySlides5 fade">
    <!--Image here-->
  </div>

  <div class="mySlides5 fade">
    <!--Image here-->
  </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(2, 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>
1 Like

Hi, bearing in mind JS is not my area it looks like you have the arrays incorrectly assigned. You have also closed the slideshow container in the wrong place and you are missing the js function for the dots that you added.

I think I’ve pieced it together correctly and this is working.

<!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:0 auto 25px;
}
/* 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;
}
.image {
	height:100px;
	background:red;
}
.img1 {
	background:red
}
.img2 {
	background:yellow
}
.img3 {
	background:green
}
</style>
</head>
<body>
<div class="slideshow-container">
  <div class="mySlides1 fade"> 
    <!--Image here-->
    <div class="image img1">Image 1-1</div>
  </div>
  <div class="mySlides1 fade"> 
    <!--Image here-->
    <div class="image img2">Image 1-2</div>
  </div>
  <div class="mySlides1 fade"> 
    <!--Image here-->
    <div class="image img3">Image 1-3</div>
  </div>
  <a class="prev" onclick="plusSlides(-1, 0)">&#10094;</a> <a class="next" onclick="plusSlides(1, 0)">&#10095;</a>
  <div style="text-align:center"> <span class="dot" onclick="currentSlide(1,0)"></span> <span class="dot" onclick="currentSlide(2,0)"></span> <span class="dot" onclick="currentSlide(3,0)"></span> </div>
</div>
<div class="slideshow-container">
  <div class="mySlides2 fade"> 
    <!--Image here-->
    <div class="image img1">Image 2-1</div>
  </div>
  <div class="mySlides2 fade"> 
    <!--Image here-->
    <div class="image img2">Image 2-2</div>
  </div>
  <div class="mySlides2 fade"> 
    <!--Image here-->
    <div class="image img3">Image 2-3</div>
  </div>
  <a class="prev" onclick="plusSlides(-1, 1)">&#10094;</a> <a class="next" onclick="plusSlides(1, 1)">&#10095;</a>
  <div style="text-align:center"> <span class="dot" onclick="currentSlide(1,1)"></span> <span class="dot" onclick="currentSlide(2,1)"></span> <span class="dot" onclick="currentSlide(3,1)"></span> </div>
</div>
<div class="slideshow-container">
  <div class="mySlides3 fade"> 
    <!--Image here-->
    <div class="image img1">Image 3-1</div>
  </div>
  <div class="mySlides3 fade"> 
    <!--Image here-->
    <div class="image img2">Image 3-2</div>
  </div>
  <div class="mySlides3 fade"> 
    <!--Image here-->
    <div class="image img3">Image 3-3</div>
  </div>
  <a class="prev" onclick="plusSlides(-1, 2)">&#10094;</a> <a class="next" onclick="plusSlides(1, 2)">&#10095;</a>
  <div style="text-align:center"> <span class="dot" onclick="currentSlide(1,2)"></span> <span class="dot" onclick="currentSlide(2,2)"></span> <span class="dot" onclick="currentSlide(3,2)"></span> </div>
</div>
<div class="slideshow-container">
  <div class="mySlides4 fade"> 
    <!--Image here-->
    <div class="image img1">Image 4-1</div>
  </div>
  <div class="mySlides4 fade"> 
    <!--Image here-->
    <div class="image img2">Image 4-2</div>
  </div>
  <div class="mySlides4 fade"> 
    <!--Image here-->
    <div class="image img3">Image 4-3</div>
  </div>
  <a class="prev" onclick="plusSlides(-1, 3)">&#10094;</a> <a class="next" onclick="plusSlides(1, 3)">&#10095;</a>
  <div style="text-align:center"> <span class="dot" onclick="currentSlide(1,3)"></span> <span class="dot" onclick="currentSlide(2,3)"></span> <span class="dot" onclick="currentSlide(3,3)"></span> </div>
</div>
<div class="slideshow-container">
  <div class="mySlides5 fade"> 
    <!--Image here-->
    <div class="image img1">Image 5-1</div>
  </div>
  <div class="mySlides5 fade"> 
    <!--Image here-->
    <div class="image img2">Image 5-2</div>
  </div>
  <div class="mySlides5 fade"> 
    <!--Image here-->
    <div class="image img3">Image 5-3</div>
  </div>
  <a class="prev" onclick="plusSlides(-1, 4)">&#10094;</a> <a class="next" onclick="plusSlides(1, 4)">&#10095;</a>
  <div style="text-align:center"> <span class="dot" onclick="currentSlide(1,4)"></span> <span class="dot" onclick="currentSlide(2,4)"></span> <span class="dot" onclick="currentSlide(3,4)"></span> </div>
</div>
<script>
var slideIndex = [1,1,1,1,1];
var slideId = ["mySlides1", "mySlides2", "mySlides3", "mySlides4", "mySlides5"]
showSlides(1, 0);
showSlides(1, 1);
showSlides(1, 2);
showSlides(1, 3);
showSlides(1, 4);

function plusSlides(n, no) {
  showSlides(slideIndex[no] += n, no);
}
function currentSlide(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>

Note that I added a div with a coloured background so that its easier to test and some text to show what element is displayed so that you can make sure each one is correct before swapping the div for the real images.

I’m not keen on the onclick handlers in the html and they should be moved to the js file. Also it would be better if the dots were automatically generated to match the number of images etc. Hopefully one of the more proficient js gurus around here will offer more advanced code should you require it :slight_smile:

1 Like

Hey Paul,

Thanks so much for your help. I just added it to the page with some pictures & it works great! Yeah it would be a little more convenient if those dots were automatically updated, but I’m just glad to have it functioning! I spent about 2 hours googling & messing with it yesterday with no luck, so you really helped me out here. I’ll work on putting those onclick handlers in a separate js file next. Thanks again!
Ryan

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.