Reputation: 39
I am using W3.css active/current tags and want to use this so that with each tab pressed a different slide show starts ( I have also used the w3.css slide show tutorial for this using the images as indicators and including a prev/next). The problem I have is how to get each tab to open up the different slide shows I have created. I have tried various ways of combining them but can only get one tab to display the slideshow and any after that will not work at all.
the codes are basically the same as the following
For the tabs https://www.w3schools.com/w3css/tryit.asp?filename=tryw3css_tabulators_active
and the slide shows
https://www.w3schools.com/w3css/tryit.asp?filename=tryw3css_slideshow_imgdots
How can I combine the two? is it even possible?
This is a bit of what I have tried so far, I also tried adding the slide show under all the other tabs too as it is in the other.
function openPics(evt, picsName) {
var i, x, tablinks;
x = document.getElementsByClassName("pics");
for (i = 0; i < x.length; i++) {
x[i].style.display = "none";
}
tablinks = document.getElementsByClassName("tablink");
for (i = 0; i < x.length; i++) {
tablinks[i].className = tablinks[i].className.replace(" w3-red", "");
}
document.getElementById(picsName).style.display = "block";
evt.currentTarget.className += " w3-red";
}
var slideIndex = 1;
showDivs(slideIndex);
function plusDivs(n) {
showDivs(slideIndex += n);
}
function currentDiv(n) {
showDivs(slideIndex = n);
}
function showDivs(n) {
var i;
var x = document.getElementsByClassName("mySlides");
var dots = document.getElementsByClassName("demo");
if (n > x.length) {
slideIndex = 1
}
if (n < 1) {
slideIndex = x.length
}
for (i = 0; i < x.length; i++) {
x[i].style.display = "none";
}
for (i = 0; i < dots.length; i++) {
dots[i].className = dots[i].className.replace(" w3-opacity-off", "");
}
x[slideIndex - 1].style.display = "block";
dots[slideIndex - 1].className += " w3-opacity-off";
}
<link rel="stylesheet" href="https://www.w3schools.com/w3css/4/w3.css">
<div class="w3-container">
<h1>KAUSI 2017</h1>
<div class="w3-bar w3-black">
<button class="w3-bar-item w3-button tablink w3-red" onclick="openPics(event,'nivala')">nivala</button>
<button class="w3-bar-item w3-button tablink" onclick="openPics(event,'oulu')">oulu</button>
<button class="w3-bar-item w3-button tablink" onclick="openPics(event,'sotkamo')">sotkamo</button>
<button class="w3-bar-item w3-button tablink" onclick="openPics(event,'practice')">practice</button>
</div>
<div id="nivala" class="w3-container w3-border pics w3-animate-zoom ">
<h3>Nivala</h3>
</div>
<div id="oulu" class="w3-container w3-border pics w3-animate-zoom" style="display:none">
<h3>oulu </h3>
<div class="w3-container">
<h1 align="center">Veteli - Nivala </h1>
<p align="center"> Vetelissä Ruohopatilla 20.6</p>
</div>
<div class="w3-center">
<div class="w3-content" style="max-width:1200px">
<img class="mySlides" src="../vepeimages/nivala20_6/nivala2.jpg" style="width:75%">
<img class="mySlides" src="../vepeimages/nivala20_6/nivala3.jpg" style="width:75%">
<img class="mySlides" src="../vepeimages/nivala20_6/nivala4.jpg" style="width:75%">
<img class="mySlides" src="../vepeimages/nivala20_6/nivala5.jpg" style="width:75%">
<div class="w3-section">
<button class="w3-btn w3-white w3-border w3-border-red w3-round-large" onclick="plusDivs(-1)">❮ Prev</button>
<button class="w3-btn w3-white w3-border w3-border-red w3-round-large" onclick="plusDivs(1)">Next ❯</button>
<div class="w3-row-padding w3-section">
<div class="w3-col s1 w3-section ">
<img class="demo w3-opacity w3-hover-opacity-off" src="../vepeimages/nivala20_6/nivala2.jpg" style="width:75%" onclick="currentDiv(1)">
</div>
<div class="w3-col s1 w3-section">
<img class="demo w3-opacity w3-hover-opacity-off" src="../vepeimages/nivala20_6/nivala3.jpg" style="width:75%" onclick="currentDiv(2)">
</div>
<div class="w3-col s1 w3-section">
<img class="demo w3-opacity w3-hover-opacity-off" src="../vepeimages/nivala20_6/nivala4.jpg" style="width:75%" onclick="currentDiv(3)">
</div>
<div class="w3-col s1 w3-section ">
<img class="demo w3-opacity w3-hover-opacity-off" src="../vepeimages/nivala20_6/nivala5.jpg" style="width:75%" onclick="currentDiv(4)">
</div>
</div>
</div>
<div id="sotkamo" class="w3-container w3-border pics w3-animate-zoom" style="display:none" <h3>sotkamo </h3>
</div>
<div id="practice" class="w3-container w3-border pics w3-animate-zoom" style="display:none">
<h3>practice </h3>
</div>
</div>
Upvotes: 1
Views: 262
Reputation: 2139
Your first problem, "the sotkamo and practice tabs are not showing", it has something to do with your closing tags. The solution is to make sure that the #sotkamo
and #practice
are same level with #nivala
and #oulu
. In your case, you are missing several closing </div>
which made the browser interpret that your #sotkamo
and #practice
were inside your #oulu
.
As for your second question, or as what I have understood, you want to have slideshows in different tabs. This is, of course, possible. However, you need to tweak your function in showing the slides. You should take into account what tab is active and then find all .slides
under that parent. You can use the parent's id as a parameter and then pass it to the function:
See this sample function:
function showDivs(parent, n) {
var i,
parentDiv = document.getElementById(parent);
var x = parentDiv.getElementsByClassName("mySlides");
var dots = parentDiv.getElementsByClassName("demo");
if (n > x.length) {
slideIndices[parent] = 1
}
if (n < 1) {
slideIndices[parent] = x.length
}
for (i = 0; i < x.length; i++) {
x[i].style.display = "none";
}
for (i = 0; i < dots.length; i++) {
dots[i].className = dots[i].className.replace(" w3-opacity-off", "");
}
x[slideIndices[parent] - 1].style.display = "block";
dots[slideIndices[parent] - 1].className += " w3-opacity-off";
}
Feel free to explore this sample snippet for your reference:
function openPics(evt, picsName) {
var i, x, tablinks;
x = document.getElementsByClassName("pics");
for (i = 0; i < x.length; i++) {
x[i].style.display = "none";
}
tablinks = document.getElementsByClassName("tablink");
for (i = 0; i < x.length; i++) {
tablinks[i].className = tablinks[i].className.replace(" w3-red", "");
}
var block = document.getElementById(picsName);
block.style.display = "block";
evt.currentTarget.className += " w3-red";
}
//default active slide is 1. update on slide change to preserve state
var slideIndices = {
oulu: 1,
sotkamo: 1
};
for (var key in slideIndices) {
if (slideIndices.hasOwnProperty(key)) {
showDivs(key, 1);
}
}
function plusDivs(parent, n) {
showDivs(parent, slideIndices[parent] += n);
}
function currentDiv(parent, n) {
showDivs(parent, slideIndices[parent] = n);
}
function showDivs(parent, n) {
var i,
parentDiv = document.getElementById(parent);
var x = parentDiv.getElementsByClassName("mySlides");
var dots = parentDiv.getElementsByClassName("demo");
if (n > x.length) {
slideIndices[parent] = 1
}
if (n < 1) {
slideIndices[parent] = x.length
}
for (i = 0; i < x.length; i++) {
x[i].style.display = "none";
}
for (i = 0; i < dots.length; i++) {
dots[i].className = dots[i].className.replace(" w3-opacity-off", "");
}
x[slideIndices[parent] - 1].style.display = "block";
dots[slideIndices[parent] - 1].className += " w3-opacity-off";
}
<link rel="stylesheet" href="https://www.w3schools.com/w3css/4/w3.css">
<div class="w3-container">
<h1>KAUSI 2017</h1>
<div class="w3-bar w3-black">
<button class="w3-bar-item w3-button tablink w3-red" onclick="openPics
(event,'nivala')">nivala</button>
<button class="w3-bar-item w3-button tablink" onclick="openPics(event,'oulu')">oulu</button>
<button class="w3-bar-item w3-button tablink" onclick="openPics
(event,'sotkamo')">sotkamo</button>
<button class="w3-bar-item w3-button tablink" onclick="openPics
(event,'practice')">practice</button>
</div>
<div id="nivala" class="w3-container w3-border pics w3-animate-zoom ">
<h3>Nivala</h3>
</div>
<div id="oulu" class="w3-container w3-border pics w3-animate-zoom" style="display:none;">
<h3>oulu </h3>
<div class="w3-container">
<h1 align="center">Veteli - Nivala </h1>
<p align="center"> Vetelissä Ruohopatilla 20.6</p>
</div>
<div class="w3-center">
<div class="w3-content" style="max-width:1200px">
<img class="mySlides" src="http://via.placeholder.com/450x150/ccc/fff" style="width:75%">
<img class="mySlides" src="http://via.placeholder.com/450x150/f5f/fff" style="width:75%">
<img class="mySlides" src="http://via.placeholder.com/450x150/4e56fe/fff" style="width:75%">
<img class="mySlides" src="http://via.placeholder.com/450x150/565f4e/fff" style="width:75%">
<div class="w3-section">
<button class="w3-btn w3-white w3-border w3-border-red w3-round-large" onclick="plusDivs('oulu',-1)">❮ Prev</button>
<button class="w3-btn w3-white w3-border w3-border-red w3-round-large" onclick="plusDivs('oulu',1)">Next ❯</button>
<div class="w3-row-padding w3-section">
<div class="w3-col s1 w3-section ">
<img class="demo w3-opacity w3-hover-opacity-off" src="http://via.placeholder.com/450x150/ccc/fff" style="width:75%" onclick="currentDiv('oulu',1)">
</div>
<div class="w3-col s1 w3-section">
<img class="demo w3-opacity w3-hover-opacity-off" src="http://via.placeholder.com/450x150/f5f/fff" style="width:75%" onclick="currentDiv('oulu',2)">
</div>
<div class="w3-col s1 w3-section">
<img class="demo w3-opacity w3-hover-opacity-off" src="http://via.placeholder.com/450x150/4e56fe/fff" style="width:75%" onclick="currentDiv('oulu',3)">
</div>
<div class="w3-col s1 w3-section ">
<img class="demo w3-opacity w3-hover-opacity-off" src="http://via.placeholder.com/450x150/565f4e/fff" style="width:75%" onclick="currentDiv('oulu',4)">
</div>
</div>
</div>
</div>
</div>
</div>
<div id="sotkamo" class="w3-container w3-border pics w3-animate-zoom" style="display:none">
<h3>sotkamo </h3>
<div class="w3-container">
<h1 align="center">Veteli - Nivala </h1>
<p align="center"> Vetelissä Ruohopatilla 20.6</p>
</div>
<div class="w3-center">
<div class="w3-content" style="max-width:1200px">
<img class="mySlides" src="http://via.placeholder.com/450x150/ccc/fff" style="width:75%">
<img class="mySlides" src="http://via.placeholder.com/450x150/f5f/fff" style="width:75%">
<img class="mySlides" src="http://via.placeholder.com/450x150/4e56fe/fff" style="width:75%">
<img class="mySlides" src="http://via.placeholder.com/450x150/565f4e/fff" style="width:75%">
<div class="w3-section">
<button class="w3-btn w3-white w3-border w3-border-red w3-round-large" onclick="plusDivs('sotkamo',-1)">❮ Prev</button>
<button class="w3-btn w3-white w3-border w3-border-red w3-round-large" onclick="plusDivs('sotkamo',1)">Next ❯</button>
<div class="w3-row-padding w3-section">
<div class="w3-col s1 w3-section ">
<img class="demo w3-opacity w3-hover-opacity-off" src="http://via.placeholder.com/450x150/ccc/fff" style="width:75%" onclick="currentDiv('sotkamo',1)">
</div>
<div class="w3-col s1 w3-section">
<img class="demo w3-opacity w3-hover-opacity-off" src="http://via.placeholder.com/450x150/f5f/fff" style="width:75%" onclick="currentDiv('sotkamo',2)">
</div>
<div class="w3-col s1 w3-section">
<img class="demo w3-opacity w3-hover-opacity-off" src="http://via.placeholder.com/450x150/4e56fe/fff" style="width:75%" onclick="currentDiv('sotkamo',3)">
</div>
<div class="w3-col s1 w3-section ">
<img class="demo w3-opacity w3-hover-opacity-off" src="http://via.placeholder.com/450x150/565f4e/fff" style="width:75%" onclick="currentDiv('sotkamo',4)">
</div>
</div>
</div>
</div>
</div>
</div>
<div id="practice" class="w3-container w3-border pics w3-animate-zoom" style="display:none">
<h3>practice </h3>
</div>
</div>
Upvotes: 1