Reputation: 21
I'm supposed to have 4 different sliders with 3 different picture on each slider. I use W3school's slideshow (https://www.w3schools.com/howto/tryit.asp?filename=tryhow_js_slideshow_multiple) to get the first two (which is working fine!) but when i tried to add 2 more the pictures won't show.
JS:
showSlides(1, 0); <-- this was from W3
showSlides(1, 1); <-- this was from W3
showSlides(1, 2); <-- I added this
showSlides(1, 3); <-- I added this
does someone know how to fix this problem?
Here is the one from W3Schools:
<div class="slideshow-container">
<div class="grantoppen-slide">
<img src="bilder/grantoppen01.jpg" style="width:100%">
</div>
<div class="grantoppen-slide">
<img src="bilder/grantoppen02.jpg" style="width:100%">
</div>
<div class="grantoppen-slide">
<img src="bilder/grantoppen03.jpg" style="width:100%">
</div>
<a class="prev" onclick="plusSlides(-1, 0)">❮</a>
<a class="next" onclick="plusSlides(1, 0)">❯</a>
</div>
And this is mine:
var slideIndex = [1, 1];
var slideId = ["grantoppen-slide", "granbo-slide", "granstua-slide", "granhaug-slide"];
showSlides(1, 0);
showSlides(1, 1);
showSlides(1, 2);
showSlides(1, 3);
function plusSlides(n, no) {
showSlides(slideIndex[no] += n, no);
}
function showSlides(n, no) {
var i;
var bildeSlide = document.getElementsByClassName(slideId[no]);
if (n > bildeSlide.length) {
slideIndex[no] = 1
}
if (n < 1) {
slideIndex[no] = bildeSlide.length
}
for (i = 0; i < bildeSlide.length; i++) {
bildeSlide[i].style.display = "none";
}
bildeSlide[slideIndex[no] - 1].style.display = "block";
}
<p id="hyttenavn-slideshow"><i id="granstua-tekst">Granstua</i></p>
<div class="slideshow-container">
<div class="granstua-slide">
<img src="bilder/granstua01.jpg" style="width:100%">
</div>
<div class="granstua-slide">
<img src="bilder/granstua02.jpg" style="width:100%">
</div>
<div class="granstua-slide">
<img src="bilder/granstua03.jpg" style="width:100%">
</div>
<a class="prev" onclick="plusSlides(-1, 2)">❮</a>
<a class="next" onclick="plusSlides(1, 2)">❯</a>
</div>
Upvotes: 2
Views: 1970
Reputation: 66
Edit:
Solution 1: the proper way would be to actually add 2 values to 'slideIndex':
var slideIndex = [1, 1, 1, 1];
Instead of:
var slideIndex = [1, 1];
And that is it. This fixes the issue of the last line going out of index with new slides.
Solution 2: If you plan to change number of slides often and to automate it, you can populate the array for each index slide when page loads with an additional initiaaliseSlides() function:
var slideIndex = []; // This will be automatically populated
var slideId = ["grantoppen-slide", "granbo-slide", "granstua-slide", "granhaug-slide"];
initialiseSlides();
showSlides(1, 0);
showSlides(1, 1);
showSlides(1, 2); // Note that this is set to 1 now
showSlides(1, 3);
// Initalise all starting slide indexes
function initialiseSlides(){
for (var i = 0; i < slideId.length; i++){
slideIndex[i] = 1;
}
}
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";
}
Solution 3: Or the showSlides() can be fixed up to correct itself like this, without using initialiseSlides(). Note the 4th line:
function showSlides(n, no) {
var i;
var x = document.getElementsByClassName(slideId[no]);
if (typeof slideIndex[no] === 'undefined') {slideIndex[no] = 1;}
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";
}
Upvotes: 1
Reputation: 28563
Well your javascript was okay, except for the slideId array. As you gave only one slider in this example, the granstua-slide
, that would have been the only one needed. However, as you asked about multiple sliders, I copied your code to make a second slider, but assigned a second class name, granstua-img
, and added that to the array.
The plusSlides (-1, 0) ( to move back and forward) should remain the same as in the initial code, so you don't need to adjust these - that's where were going wrong regarding navigation
Hope this helps
var slideIndex = [1, 1];
var slideId = ["granstua-slide", "granstua-img"];
showSlides(1, 0);
showSlides(1, 1);
showSlides(1, 2);
showSlides(1, 3);
function plusSlides(n, no) {
showSlides(slideIndex[no] += n, no);
}
function showSlides(n, no) {
var i;
var bildeSlide = document.getElementsByClassName(slideId[no]);
if (n > bildeSlide.length) {slideIndex[no] = 1}
if (n < 1) {slideIndex[no] = x.length}
for (i = 0; i < bildeSlide.length; i++) {
bildeSlide[i].style.display = "none";
}
bildeSlide[slideIndex[no]-1].style.display = "block";
}
* {box-sizing: border-box}
.grantstua-slide {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;
}
<p id="hyttenavn-slideshow"><i id="granstua-tekst">Granstua</i></p>
<div class="slideshow-container">
<div class="granstua-slide">
<img src="http://lorempixel.com/600/400/fashion" style="width:100%">
</div>
<div class="granstua-slide">
<img src="http://lorempixel.com/600/400/cats" style="width:100%">
</div>
<div class="granstua-slide">
<img src="http://lorempixel.com/600/400/food" style="width:100%">
</div>
<a class="prev" onclick="plusSlides(-1, 0)">❮</a>
<a class="next" onclick="plusSlides(1, 0)">❯</a>
</div>
<p id="hyttenavn-slideshow"><i id="granstua-tekst">Slidehow 2</i></p>
<div class="slideshow-container">
<div class="granstua-img">
<img src="http://lorempixel.com/600/400/food" style="width:100%">
</div>
<div class="granstua-img">
<img src="http://lorempixel.com/600/400/fashion" style="width:100%">
</div>
<div class="granstua-img">
<img src="http://lorempixel.com/600/400/cats" style="width:100%">
</div>
<a class="prev" onclick="plusSlides(-1, 1)">❮</a>
<a class="next" onclick="plusSlides(1, 1)">❯</a>
</div>
Upvotes: 0