Reputation: 51
I'm trying to run 3 slideshows on one page. I tried the following the code, But when I click the buttons on the 3rd slideshow, It changes the images on the 2nd slideshow. Please help me to solve this issue
var slideIndex = [1, 1, 1];
var slideId = ["mySlides1", "mySlides2", "mySlides3"]
showDivs(1, 0);
showDivs(1, 1);
showDivs(1, 2);
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";
}
.mySlides {display:none;}
<head>
<link rel="stylesheet" href="https://www.w3schools.com/w3css/4/w3.css">
<body>
<div class="w3-content w3-display-container">
<img class="mySlides1" src="https://www.w3schools.com/w3css/img_snowtops.jpg" style="width:100%">
<img class="mySlides1" src="https://www.w3schools.com/w3css/img_lights.jpg" style="width:100%">
<img class="mySlides1" src="https://www.w3schools.com/w3css/img_mountains.jpg" style="width:100%">
<img class="mySlides1" src="https://www.w3schools.com/w3css/img_forest.jpg" style="width:100%">
<button class="w3-button w3-black w3-display-left" onclick="plusDivs(-1, 0)">❮</button>
<button class="w3-button w3-black w3-display-right" onclick="plusDivs(1, 0)">❯</button>
</div>
<br>
<div class="w3-content w3-display-container">
<img class="mySlides2" src="https://www.w3schools.com/w3css/img_la.jpg" style="width:100%">
<img class="mySlides2" src="https://www.w3schools.com/w3css/img_ny.jpg" style="width:100%">
<img class="mySlides2" src="https://www.w3schools.com/w3css/img_chicago.jpg" style="width:100%">
<button class="w3-button w3-black w3-display-left" onclick="plusDivs(-1, 1)">❮</button>
<button class="w3-button w3-black w3-display-right" onclick="plusDivs(1, 1)">❯</button>
</div>
<br>
<div class="w3-content w3-display-container">
<img class="mySlides3" src="https://www.w3schools.com/w3css/img_la.jpg" style="width:100%">
<img class="mySlides3" src="https://www.w3schools.com/w3css/img_ny.jpg" style="width:100%">
<img class="mySlides3" src="https://www.w3schools.com/w3css/img_chicago.jpg" style="width:100%">
<button class="w3-button w3-black w3-display-left" onclick="plusDivs(-1, 1)">❮</button>
<button class="w3-button w3-black w3-display-right" onclick="plusDivs(1, 1)">❯</button>
</div>
Upvotes: 0
Views: 115
Reputation: 1119
in plusDivs second parameter is index of slider , in third slider you are selecting second slider thats why second slider is changing see image below
Upvotes: 1
Reputation: 2217
var slideIndex = [1, 1, 1];
var slideId = ["mySlides1", "mySlides2", "mySlides3"]
showDivs(1, 0);
showDivs(1, 1);
showDivs(1, 2);
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";
}
.mySlides {display:none;}
<head>
<link rel="stylesheet" href="https://www.w3schools.com/w3css/4/w3.css">
<body>
<div class="w3-content w3-display-container">
<img class="mySlides1" src="https://www.w3schools.com/w3css/img_snowtops.jpg" style="width:100%">
<img class="mySlides1" src="https://www.w3schools.com/w3css/img_lights.jpg" style="width:100%">
<img class="mySlides1" src="https://www.w3schools.com/w3css/img_mountains.jpg" style="width:100%">
<img class="mySlides1" src="https://www.w3schools.com/w3css/img_forest.jpg" style="width:100%">
<button class="w3-button w3-black w3-display-left" onclick="plusDivs(-1, 0)">❮</button>
<button class="w3-button w3-black w3-display-right" onclick="plusDivs(1, 0)">❯</button>
</div>
<br>
<div class="w3-content w3-display-container">
<img class="mySlides2" src="https://www.w3schools.com/w3css/img_la.jpg" style="width:100%">
<img class="mySlides2" src="https://www.w3schools.com/w3css/img_ny.jpg" style="width:100%">
<img class="mySlides2" src="https://www.w3schools.com/w3css/img_chicago.jpg" style="width:100%">
<button class="w3-button w3-black w3-display-left" onclick="plusDivs(-1, 1)">❮</button>
<button class="w3-button w3-black w3-display-right" onclick="plusDivs(1, 1)">❯</button>
</div>
<br>
<div class="w3-content w3-display-container">
<img class="mySlides3" src="https://www.w3schools.com/w3css/img_la.jpg" style="width:100%">
<img class="mySlides3" src="https://www.w3schools.com/w3css/img_ny.jpg" style="width:100%">
<img class="mySlides3" src="https://www.w3schools.com/w3css/img_chicago.jpg" style="width:100%">
<button class="w3-button w3-black w3-display-left" onclick="plusDivs(-1, 2)">❮</button>
<button class="w3-button w3-black w3-display-right" onclick="plusDivs(1, 2)">❯</button>
</div>
Upvotes: 0