Multiple Slideshow not working properly. Any Clue? - javascript

So basically, I'm trying to create multiple slideshows on the page with dots to switch between the slides (because I've already tried the arrow type button and it doesn't workout for me idk), and the issue is that when I use more than one slideshow the dots on the first one either stop working or begin switching the below slideshows slides.
I have already seen a lot of threads about this issue and tried several answers to them, but none of them seem to work. So if anybody has any clue of what is happening in my code and knows the answer, it would be great. PD: I'm sorry if it contains any grammatic faults :c.
This is the code I'm using (originally from w3s: https://www.w3schools.com/howto/tryit.asp?filename=tryhow_js_quotes_slideshow and https://www.w3schools.com/howto/tryit.asp?filename=tryhow_js_slideshow_multiple)
/* Referencia miscelánea de assets
color cuadraos: #31572C
color fondo global: #B3B882
color fondo variable: #90A970
color fondo recuadros: #4F772D
color fondo titulo principal&Home: #132A13
color flechas: #000000
*/
* {
font-family: 'Roboto', sans-serif;
font-size:large;
}
details {
border: 1px solid #d4d4d400;
margin-top: 10px;
}
summary {
font-weight: bold;
font-size: 120%;
margin: -.75em -.75em 0;
padding: .75em;
background-color:#31572C;
color: #fff;
}
details[open] {
padding: 10px;
}
details[open] summary {
margin-bottom: 10px;
}
#chingue {
text-align: center;
}
/* Cuadrados principales*/
.TituloEleccion {
color: #ffffff;
background-color: #31572C;
text-align: center;
height: 99%;
width:100%;
margin-top:0%;
line-height: 3.1;
}
body {
background-color: #B3B882;
}
#titulogeneral {
color: #ffffff;
background-color: #132A13;
border-style: ridge;
border-color: #4eaa4e;
margin-left: 20%;
margin-right: 20%;
height: 8.5%;
text-align: center;
line-height: 4.15;
white-space: nowrap;
overflow: hidden;
white-space: initial;
}
.colordesconocido {
background-color: #4F772D;
width:100%;
margin-top:0%;
margin-bottom: 30px;
}
#fondouniversal {
background-color:#B3B882 ;
}
.divinformacion {
width: 50%;
display: block;
float: left;
padding: 20px;
border: 2px solid rgb(0, 0, 0);
border-style: double;
border-radius: 10px;
}
.fondoeleccion {
color: #000000;
background-color: #4F772D;
width:100%;
margin-top:0%;
border: 1.5px solid rgb(0, 0, 0);
border-style: double;
margin-bottom: 30px;
}
img{
border:2px solid #000000;
}
body {
font-family:"Pathway Gothic One", sans-serif;
}
.hidecontent {
display: none;
}
#myaccordion label {
box-shadow:0 0 20px #d4d4d4;
display: block;
padding: 8px 22px;
margin: 1px 0px 1px 0px;
cursor: pointer;
background: #31572C;
color: #FFF;
transition: ease .5s;
text-align:center;
}
#myaccordion label:hover {
background: #31572C;
}
.content {
box-shadow:0 0 20px #d4d4d4;
background: #90A970;
padding: 1.5% 1.5%;
border: 1px solid #d4d4d4;
margin: -1 0 0 0;
}
#myaccordion input:checked + label + .content {
display: block;
-moz-animation: fadeIn 0.5s ease-out;
-o-animation: fadeIn 0.5s ease-out;
animation: fadeIn 0.5s ease-out;
}
#info1 {
float:right;
}
#foto {
float:left;
}
* {box-sizing: border-box}
body {font-family: Verdana, sans-serif; margin:0}
/* Slideshow container */
.slideshow-container0 {
position: relative;
background:#90A970;
}
.slideshow-container1 {
position: relative;
background:#90A970;
}
.slideshow-container2 {
position: relative;
background:#90A970;
}
/* Slides */
.mySlides0 {
display: none;
padding: 2px;
text-align: center;
}
.mySlides1 {
display: none;
padding: 2px;
text-align: center;
}
.mySlides2 {
display: none;
padding: 2px;
text-align: center;
}
/* The dot/bullet/indicator container */
.dot-container0 {
text-align: center;
padding: 20px;
background:#31572C;
}
.dot-container1 {
text-align: center;
padding: 20px;
background:#31572C;
}
.dot-container2 {
text-align: center;
padding: 20px;
background:#31572C;
}
/* 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;
}
.dot0 {
cursor: pointer;
height: 15px;
width: 15px;
margin: 0 2px;
background-color: #bbb;
border-radius: 50%;
display: inline-block;
transition: background-color 0.6s ease;
}
.dot1 {
cursor: pointer;
height: 15px;
width: 15px;
margin: 0 2px;
background-color: #bbb;
border-radius: 50%;
display: inline-block;
transition: background-color 0.6s ease;
}
.dot2 {
cursor: pointer;
height: 15px;
width: 15px;
margin: 0 2px;
background-color: #bbb;
border-radius: 50%;
display: inline-block;
transition: background-color 0.6s ease;
}
/* Add a background color to the active dot/circle */
.active, .dot:hover {
background-color: #717171;
}
.titulilloparque {
text-align: center;
color:#000000;
}
<html>
<head>
<title>PARQUES NATURALES Y NACIONALES. ANDALUCIA</title>
<link rel="stylesheet" href="CSSMAIN.css">
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=Roboto&display=swap" rel="stylesheet">
</head>
<a id="inicio"></a>
<body id="fondouniversal">
<div>
<p><h1 id="titulogeneral">PARQUES NATURALES Y NACIONALES. ANDALUCIA</h1></p>
</div>
<div id="myaccordion">
<input type="checkbox" id="accordion1" class="hidecontent"/>
<label for="accordion1"><h2>PARQUES NATURALES</h2></label>
<div class="content hidecontent">
<div class="fondoeleccion">
<h3 class="titulilloparque">PARQUE NATURAL DE SIERRA NEVADA</h3>
<div class="slideshow-container0">
<div class="mySlides0 fade">
<img src="images/FotoSN1.jpg" style="width:65%" style="height:25%"/>
<div>Imagen del parque</div>
</div>
<div class="mySlides0">
<p> <h4>DATOS</h4>
GEOLOCALIZACIÓN: 37°05'19.8''N 3°10'18.7''O <br/>
Hotel, casa rural y cabaña.<br/>
Senderismo, alpinismo, rutas en bicicleta, esquí, snowboard,
clases de esquí y snowboard guiadas<br/>
Jabalí, gato montés, zorro, comadreja, garduña, gineta,
y el frecuente turón común <br/>
</p>
</div>
<div class="mySlides0">
<p> <h4>TURISMO</h4>
TOTAL<br/>
VIAJEROS 3712<br/>
PERNOCTACIONES 8430<br/>
EST. MEDIA 2,27 d<br/><br/>
ESPAÑOLES<br/>
VIAJEROS 2235<br/>
PERNOCTACIONES 5558<br/>
EST. MEDIA 2,49 d<br/><br/>
EXTRANJEROS<br/>
VIAJEROS 1477<br/>
PERNOCTACIONES 2872<br/>
EST. MEDIA 1,94 d<br/>
</p>
</div>
</div>
<div class="dot-container0">
<span class="dot0" onclick="moverprimero(1)"></span>
<span class="dot0" onclick="moverprimero(2)"></span>
<span class="dot0" onclick="moverprimero(3)"></span>
</div>
<script>
var slideIndex1 = 2;
showSlides(slideIndex1);
function plusSlides(n) {
showSlides(slideIndex1 += n);
}
function moverprimero(n) {
showSlides(slideIndex1 = n);
}
function showSlides(n) {
var i;
var slides = document.getElementsByClassName("mySlides0");
var dots = document.getElementsByClassName("dot0");
if (n > slides.length) {slideIndex1 = 1}
if (n < 1) {slideIndex1 = slides.length}
for (i = 0; i < slides.length; i++) {
slides[i].style.display = "none";
}
for (i = 0; i < dots.length; i++) {
dots[i].className = dots[i].className.replace(" active", "");
}
slides[slideIndex1-1].style.display = "block";
dots[slideIndex1-1].className += " active";
}
</script>
🏚
</div>
<div class="fondoeleccion">
<h3 class="titulilloparque">PARQUE NATURAL SIERRA DE LAS NIEVES</h3>
<div class="slideshow-container1">
<div class="mySlides1 fade">
<img src="images/FOTOSdN1.jpg" style="width:65%" style="height:25%">
<div>Imagen del parque</div>
</div>
<div class="mySlides1">
<p> <h4>DATOS</h4>
GEOLOCALIZACIÓN: 36°44'N 4°59'O <br>
Hotel y casa rural.<br>
Rutas de senderismo, rutas en bicicleta,
rutas en 4x4 y observatorio de aves.<br>
Íbice ibérico, gato montés, zorro, comadreja,
garduña, gineta, turón común.<br>
</p>
</div>
<div class="mySlides1">
<p> <h4>TURISMO</h4>
TOTAL<br>
VIAJEROS 2928<br>
PERNOCTACIONES 5415<br>
EST. MEDIA 1'85 d<br><br>
ESPAÑOLES<br>
VIAJEROS 1245<br>
PERNOCTACIONES 2041<br>
EST. MEDIA 1'64 d<br><br>
EXTRANJEROS<br>
VIAJEROS 1683<br>
PERNOCTACIONES 3374<br>
EST. MEDIA 2 d<br>
</p>
</div>
</div>
<div class="dot-container1">
<span class="dot1" onclick="moversegundo(1)"></span>
<span class="dot1" onclick="moversegundo(2)"></span>
<span class="dot1" onclick="moversegundo(3)"></span>
</div>
<script>
var slideIndex2 = 1;
showSlides(slideIndex2);
function plusSlides(n) {
showSlides(slideIndex2 += n);
}
function moversegundo(n) {
showSlides(slideIndex2 = n);
}
function showSlides(n) {
var i;
var slides = document.getElementsByClassName("mySlides1");
var dots = document.getElementsByClassName("dot1");
if (n > slides.length) {slideIndex2 = 1}
if (n < 1) {slideIndex2 = slides.length}
for (i = 0; i < slides.length; i++) {
slides[i].style.display = "none";
}
for (i = 0; i < dots.length; i++) {
dots[i].className = dots[i].className.replace(" active", "");
}
slides[slideIndex2-1].style.display = "block";
dots[slideIndex2-1].className += " active";
}
</script>
🏚
</div>
<div class="fondoeleccion">
<h3 class="titulilloparque">PARQUE NATURAL DE LA BREÑA Y MARISMAS DE BARBATE</h3>
<div class="slideshow-container2">
<div class="mySlides2 fade">
<img src="images/FOTOBMB1.jpg" style="width:65%" style="height:25%">
<div>Imagen del parque</div>
</div>
<div class="mySlides2">
<p> <h4>DATOS</h4>
GEOLOCALIZACIÓN: 36°10'44''N 5°58'22''O <br>
Hotel y casa rural.<br>
Inmersión submarina, surf, observación de aves,
rutas de senderimos y la destaca visita al tajo vertical de
los Acantilados de Barbate.<br>
Principalmente fauna aviar; gaviota argéntea,
garcilla bueyera, garcetas, vencejo, mirlo, cárabo, carbonero, etc.<br>
</p>
</div>
<div class="mySlides2">
<p> <h4>TURISMO</h4>
TOTAL<br>
VIAJEROS 2188<br>
PERNOCTACIONES 7363<br>
EST. MEDIA 3,37 d<br><br>
ESPAÑOLES<br>
VIAJEROS 1417<br>
PERNOCTACIONES 3744<br>
EST. MEDIA 2,64 d<br><br>
EXTRANJEROS<br>
VIAJEROS 771<br>
PERNOCTACIONES 3619<br>
EST. MEDIA 4,69 d<br>
</p>
</div>
</div>
<div class="dot-container2">
<span class="dot2" onclick="movertercero(1)"></span>
<span class="dot2" onclick="movertercero(2)"></span>
<span class="dot2" onclick="movertercero(3)"></span>
</div>
<script>
var slideIndex3 = 1;
showSlides(slideIndex3);
function movertercero(n) {
showSlides(slideIndex3 = n);
}
function showSlides(n) {
var i;
var slides = document.getElementsByClassName("mySlides2");
var dots = document.getElementsByClassName("dot2");
if (n > slides.length) {slideIndex3 = 1}
if (n < 1) {slideIndex3 = slides.length}
for (i = 0; i < slides.length; i++) {
slides[i].style.display = "none";
}
for (i = 0; i < dots.length; i++) {
dots[i].className = dots[i].className.replace(" active", "");
}
slides[slideIndex3-1].style.display = "block";
dots[slideIndex3-1].className += " active";
}
</script>
🏚
</div>
</div>
</body>
</html>

This code wont work because there's a problem with
your JavaScript as some variables are conflicting
I will suggest you to make separate JavaScript file first and include it in your index.html like this ;
<scritp src=" *your JavaScript file path* "> </script>
this way your code is separated and it'll be easy to determine problems in your code.
well after this follow the same pattern as per link you provided of w3cSchool.
https://www.w3schools.com/howto/tryit.asp?filename=tryhow_js_quotes_slideshow and https://www.w3schools.com/howto/tryit.asp?filename=tryhow_js_slideshow_multiple)
commented out things that you should resolve according to your requirement;
these first two are the default one 3rd is my added one just like this you have to add yourself in the code
<p>Slideshow 1:</p>
<div class="slideshow-container">
<div class="mySlides1">
<img src="img_nature_wide.jpg" style="width:100%">
</div>
<div class="mySlides1">
<img src="img_snow_wide.jpg" style="width:100%">
</div>
<div class="mySlides1">
<img src="img_mountains_wide.jpg" style="width:100%">
</div>
<a class="prev" onclick="plusSlides(-1, 0)">❮</a>
<a class="next" onclick="plusSlides(1, 0)">❯</a>
</div>
<p>Slideshow 2:</p>
<div class="slideshow-container">
<div class="mySlides2">
<img src="img_band_chicago.jpg" style="width:100%">
</div>
<div class="mySlides2">
<img src="img_band_la.jpg" style="width:100%">
</div>
<div class="mySlides2">
<img src="img_band_ny.jpg" style="width:100%">
</div>
<a class="prev" onclick="plusSlides(-1, 1)">❮</a>
<a class="next" onclick="plusSlides(1, 1)">❯</a>
</div>
<p>Slideshow 3:</p>
<div class="slideshow-container">
<div class="mySlides3"> //NEW CLASS ADDED further more class can be added like this like 4 5 6....and so on
<img src="img_band_chicago.jpg" style="width:100%">
</div>
<div class="mySlides3">
<img src="img_band_la.jpg" style="width:100%">
</div>
<div class="mySlides3">
<img src="img_band_ny.jpg" style="width:100%">
</div>
here I added new number 2 as first two have 0 and 1 now 2 for third slide and 3 4 5.. and so on for more slides to be added watch out these should be added on y-axis as x-axis is responsible for no of jumps one slide should make
<a class="prev" onclick="plusSlides(-1, 2)">❮</a>
<a class="next" onclick="plusSlides(1, 2)">❯</a>
</div>
<script>
var slideIndex = [1, 1, 1]; //here array with value will be added for further increase in slides
var slideId = ["mySlides1", "mySlides2", "mySlides3"] // new classes will be added here
showSlides(0, 0);
showSlides(1, 1);
showSlides(0, 2); //this will increase as well depending upon the no. of slides you'll add
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>

Related

javascript carousel not showing first slide

So i took this code off of w3schools to try and learn how to build a carousel from scratch and adapted IT to my own project but i've run into a problem where it won't show the first slide by default when the page loads. the carousel works fine otherwise once you click on the balls(dots?) meant to control it.
DEFAULT SLIDE NOT SHOWING
here is the html, css and javascript code..
let slideInd = 1;
showSlides(slideInd);
function showSlides(input){
let slides = document.getElementsByClassName("slides");
let balls = document.getElementsByClassName("balls");
if (input > slides.length) {slideInd = 1}
if (input < 1 ) {slideInd = slides.length}
for (let i=0; i < slides.length; i++){
slides[i].style.display = "none";
}
for (i=0; i < balls.length; i++){
balls[i].className = balls[i].className.replace(" active","");
}
slides[slideInd-1].style.display = "block";
balls[slideInd-1].className += " active";
}
function currentSlide(input) {
showSlides(slideInd = input);
}
.sw-containter{
max-width: 1560px;
position: relative;
margin: auto;
}
.slides{
display: none;
}
.slides img{
max-width: 1535px;
vertical-align: middle;
}
.text{
background-color: #000000;
color: white;
font-family: 'Source Code Pro', monospace;
font-weight: 400;
text-align: center;
position: absolute;
width: 1535px;
}
.balls{
cursor: pointer;
display: inline-block;
position: relative;
height: 10px;
width: 10px;
margin: 45px 3px;
background-color: #000000;
border-radius: 50%;
transition: background-color 0.6s ease;
}
.active, .balls:hover{
background-color: #D9D9D9;
}
.fade{
animation-name: fade;
animation-duration: 1s;
}
#keyframes fade{
from{opacity: .4;}
to {opacity: 1;}
}
<div class="sw-container">
<div class="slides fade">
<img src="/assets/imgs/fifa23.jpg" style="width:100%;">
<div class="text">Released on September 30th! Buy now!</div>
</div>
<div class="slides fade">
<img src="/assets/imgs/starocean23.webp" style="width:100%">
<div class="text">Releases on Octber 27th. Preorder Now!</div>
</div>
<div class="slides fade">
<img src="/assets/imgs/GOWcontroller.jpg" style="width:100%">
<div class="text">Preorders live now!</div>
</div>
<div class="slides fade">
<img src="/assets/imgs/gothamKnights23.jpg" style="width:100%">
<div class="text">Releases on October 21st!</div>
</div>
<div class="slides fade">
<img src="/assets/imgs/plagueTale23.jpg" style="width:100%">
<div class="text">Releases on October 18th!</div>
</div>
</div>
<!--slider balls-->
<div style="text-align:center;">
<span class="balls" onclick="currentSlide(1)"></span>
<span class="balls" onclick="currentSlide(2)"></span>
<span class="balls" onclick="currentSlide(3)"></span>
<span class="balls" onclick="currentSlide(4)"></span>
<span class="balls" onclick="currentSlide(5)"></span>
</div>
Any help is greatly appreciated!
NOTE: i have a display: none; line in CSS meant to stop all the slides from showing up at once, if i remove it they stack up on the page so i don't think that's the problem.
Tried changing the loops to see if i had typed something wrong but it didn't work
If you are using an external JavaScript file then you can call the function by adding event listeners in the JavaScript file.
Otherwise, you need to create tag and write the whole javascript code. Then it will work.
let slides = document.getElementsByClassName("slides");
let balls = document.getElementsByClassName("balls");
[...balls].forEach((element, index) => {
element.addEventListener("click", () => currentSlide(index + 1))
})
let slideInd = 1;
showSlides(slideInd);
function showSlides(input){
if (input > slides.length) {slideInd = 1}
if (input < 1 ) {slideInd = slides.length}
for (let i=0; i < slides.length; i++){
slides[i].style.display = "none";
}
for (let i=0; i < balls.length; i++){
balls[i].className = balls[i].className.replace(" active","");
}
slides[slideInd-1].style.display = "block";
balls[slideInd-1].className += " active";
}
function currentSlide(input) {
showSlides(slideInd = input);
}
.sw-containter{
max-width: 1560px;
position: relative;
margin: auto;
}
.slides{
display: none;
}
.slides img{
max-width: 1535px;
vertical-align: middle;
}
.text{
background-color: #000000;
color: white;
font-family: 'Source Code Pro', monospace;
font-weight: 400;
text-align: center;
position: absolute;
width: 1535px;
}
.balls{
cursor: pointer;
display: inline-block;
position: relative;
height: 10px;
width: 10px;
margin: 45px 3px;
background-color: #000000;
border-radius: 50%;
transition: background-color 0.6s ease;
}
.active, .balls:hover{
background-color: #D9D9D9;
}
.fade{
animation-name: fade;
animation-duration: 1s;
}
#keyframes fade{
from{opacity: .4;}
to {opacity: 1;}
}
<div class="sw-container">
<div class="slides fade">
<img src="https://images.unsplash.com/photo-1472214103451-9374bd1c798e?ixlib=rb-4.0.3&ixid=MnwxMjA3fDB8MHxzZWFyY2h8Mnx8cGljfGVufDB8fDB8fA%3D%3D&w=1000&q=80" style="width:100%;">
<div class="text">Released on September 30th! Buy now!</div>
</div>
<div class="slides fade">
<img src="https://i.pinimg.com/736x/f7/75/7d/f7757d5977c6ade5ba352ec583fe8e40.jpg" style="width:100%">
<div class="text">Releases on Octber 27th. Preorder Now!</div>
</div>
<div class="slides fade">
<img src="https://images.pexels.com/photos/670720/pexels-photo-670720.jpeg?auto=compress&cs=tinysrgb&dpr=1&w=500" style="width:100%">
<div class="text">Preorders live now!</div>
</div>
</div>
<!--slider balls-->
<div style="text-align:center;">
<span class="balls"></span>
<span class="balls"></span>
<span class="balls"></span>
</div>

How would I approach adding automation to a slideshow?

I built a JS slideshow with clickable indicators. I would like for my slideshow to play automatically, while maintaining the functionality of the indicator buttons.
In the code example below, I attempted to add a setTimeout() method to change the image every 2000ms, but it seems to not be working. What would be the best approach to change the JS so I have some automation? I also attempted to use the setInterval method with no luck. I will provide the code below:
Thank you in advance for the help/tips/and advice
var editorialSlideIndex = 1;
showEditorialSlides(editorialSlideIndex);
//Next/previous controls
function plusSlides(n) {
showEditorialSlides(editorialSlideIndex += n);
}
//Thumbnail image controls
function currentSlide(n) {
showEditorialSlides(editorialSlideIndex = n);
}
function showEditorialSlides(n) {
var i;
var slides = document.getElementsByClassName("editorial-slideshow");
var slideDotInd = document.getElementsByClassName("slideDotInd");
if (n > slides.length) {
editorialSlideIndex = 1
}
if (n < 1) {
editorialSlideIndex = slides.length
}
for (i = 0; i < slides.length; i++) {
slides[i].style.display = "none";
}
for (i = 0; i < slideDotInd.length; i++) {
slideDotInd[i].className = slideDotInd[i].className.replace(" active-slider", "");
}
slides[editorialSlideIndex - 1].style.display = "block";
slideDotInd[editorialSlideIndex - 1].className += " active-slider";
setTimeout(showEditorialSlides, 2000); // Change image every 2 seconds
}
/*Slideshow Container */
.editorial-slideshow-container {
padding-top: 15px;
position: relative;
margin: auto;
}
/*Default Hide Images*/
.editorial-slideshow {
display: none;
}
/*Next and previous buttons*/
.prev,
.next {
cursor: pointer;
position: absolute;
top: 50%;
width: auto;
margin-top: -34px;
padding: 10px;
color: white;
font-weight: bold;
font-size: 34px;
transition: 0.6s ease;
border-radius: 0 3px 3px 0;
user-select: none;
background-color: rgba(0, 128, 115, .5);
}
/* Position "next button" */
.next {
right: 0;
border-radius: 3px 0 0 3px;
}
/* On hover, background color */
.prev:hover,
.next:hover {
background-color: rgba(0, 128, 115, .8);
}
/* Caption text
.text {
color: #f2f2f2;
font-size: 15px;
padding: 8px 12px;
position: absolute;
bottom: 8px;
width: 100%;
text-align: center;
}
Number text (1/3 etc)
.numbertext {
color: #f2f2f2;
font-size: 12px;
padding: 8px 12px;
position: absolute;
top: 15;
}
*/
/* The slideslideDotInd/bullets/indicators */
.slideDotInd {
cursor: pointer;
height: 15px;
width: 15px;
margin: 15px 2px;
background-color: #bbb;
border-radius: 50%;
display: inline-block;
transition: background-color 0.6s ease;
}
.active-slider,
.slideDotInd:hover {
background-color: #717171;
}
/* Fading animation */
.fade {
-webkit-animation-name: fade;
-webkit-animation-duration: 1.5s;
animation-name: fade;
animation-duration: .25s;
}
#-webkit-keyframes fade {
from {
opacity: .8
}
to {
opacity: 1
}
}
#keyframes fade {
from {
opacity: .8
}
to {
opacity: 1
}
}
#media only screen and (max-width: 425px) {
#CGConainer .banner-title H1 {
line-height: 50px;
}
}
#media only screen and (min-width: 768px) {
.editorial-slideshow-container {
width: 75%;
/*Max width is 576px*/
}
}
#media only screen and (min-width: 1024px) {
.editorial-slideshow-container {
width: 50%;
}
}
<div class="editorial-slideshow-container">
<div class="editorial-slideshow fade">
<img src="https://storage.googleapis.com/img.triggermail.io/hammacher/slider-1.jpg" style="width:100%">
</div>
<div class="editorial-slideshow fade">
<img src="https://storage.googleapis.com/img.triggermail.io/hammacher/slider-2.jpg" style="width:100%">
</div>
<div class="editorial-slideshow fade">
<img src="https://storage.googleapis.com/img.triggermail.io/hammacher/slider-3.jpg" style="width:100%">
</div>
<div class="editorial-slideshow fade">
<img src="https://storage.googleapis.com/img.triggermail.io/hammacher/slider-4a.jpg" style="width:100%">
</div>
<div class="editorial-slideshow fade">
<img src="https://storage.googleapis.com/img.triggermail.io/hammacher/slider-5a.jpg" style="width:100%">
</div>
<div class="editorial-slideshow fade">
<img src="https://storage.googleapis.com/img.triggermail.io/hammacher/slider-6.jpg" style="width:100%">
</div>
<div class="editorial-slideshow fade">
<img src="https://storage.googleapis.com/img.triggermail.io/hammacher/slider-7.jpg" style="width:100%">
</div>
<div class="editorial-slideshow fade">
<img src="https://storage.googleapis.com/img.triggermail.io/hammacher/slider-8.jpg" style="width:100%">
</div>
<div class="editorial-slideshow fade">
<img src="https://storage.googleapis.com/img.triggermail.io/hammacher/slider-9.jpg" style="width:100%">
</div>
<div class="editorial-slideshow fade">
<img src="https://storage.googleapis.com/img.triggermail.io/hammacher/slider-10.jpg" style="width:100%">
</div>
<p class="prev" onclick="plusSlides(-1)">‹</p>
<p class="next" onclick="plusSlides(1)">›</p>
</div>
<div style="text-align:center">
<span class="slideDotInd" onclick="currentSlide(1)"></span>
<span class="slideDotInd" onclick="currentSlide(2)"></span>
<span class="slideDotInd" onclick="currentSlide(3)"></span>
<span class="slideDotInd" onclick="currentSlide(4)"></span>
<span class="slideDotInd" onclick="currentSlide(5)"></span>
<span class="slideDotInd" onclick="currentSlide(6)"></span>
<span class="slideDotInd" onclick="currentSlide(7)"></span>
<span class="slideDotInd" onclick="currentSlide(8)"></span>
<span class="slideDotInd" onclick="currentSlide(9)"></span>
<span class="slideDotInd" onclick="currentSlide(10)"></span>
</div>
It's such a small change.
The way you called setTimeout is wrong.
function showEditorialSlides(n) {
var i;
var slides = document.getElementsByClassName("editorial-slideshow");
var slideDotInd = document.getElementsByClassName("slideDotInd");
if (n > slides.length) {
editorialSlideIndex = 1
}
if (n < 1) {
editorialSlideIndex = slides.length
}
for (i = 0; i < slides.length; i++) {
slides[i].style.display = "none";
}
for (i = 0; i < slideDotInd.length; i++) {
slideDotInd[i].className = slideDotInd[i].className.replace(" active-slider", "");
}
slides[editorialSlideIndex - 1].style.display = "block";
slideDotInd[editorialSlideIndex - 1].className += " active-slider";
setTimeout(() => showEditorialSlides(editorialSlideIndex = (n+1)), 2000); // Change image every 2 seconds
}
And that was the reason your function was being called incorrectly.
EDIT: I forgot to mention the reason. There was no way JavaScript knew what parameters you were passing to the function when you're trying to recurse.
You can try it out in the fiddle

This image slider starts with nothing why?

So Ii made an image slider for a school project and this is happening when I start up the page or when I reload it. The page only shows the arrows and the dots.
Screenshot of the problem
Here is my code:
showSlides(slideIndex);
// Next/previous controls
function plusSlides(n) {
showSlides(slideIndex += n);
}
// Thumbnail image controls
function currentSlide(n) {
showSlides(slideIndex = n);
}
function showSlides(n) {
var i;
var slides = document.getElementsByClassName("mySlides");
var dots = document.getElementsByClassName("dot");
if (n > slides.length) {
slideIndex = 1
}
if (n < 1) {
slideIndex = slides.length
}
for (i = 0; i < slides.length; i++) {
slides[i].style.display = "block";
}
for (i = 0; i < dots.length; i++) {
dots[i].className = dots[i].className.replace(" active", "");
}
slides[slideIndex - 1].style.display = "block";
dots[slideIndex - 1].className += "active";
}
margin-top: -8px;
margin-left: -8px;
}
.header {
margin-top: 25px;
}
.headerimg {
width: 2047px;
margin-bottom: -168px;
}
.toDHL {
color: red;
margin-left: 81px;
}
.menu-bar {
color: black;
text-decoration: none;
font-size: 35px;
font-family: Arial;
font-weight: bold;
}
.linkimg {
width: 537px;
height: 150px;
margin-top: -89px;
margin-left: 17px;
}
#toHome {
margin-left: 50px;
border-bottom: 3px solid red;
}
#toRooster {
margin-left: 200px;
}
#toSmoelenboek {
margin-left: 200px;
}
#toSuccesverhaal {
margin-left: 200px;
position: absolute;
}
.slideshow-container {
float: left;
}
* {
box-sizing: border-box
}
/* Slideshow container */
.slideshow-container {
max-width: 1000px;
position: relative;
margin-top: 60px;
}
/* Hide the images by default */
.mySlides {
display: none;
}
/* Next & previous buttons */
.prev,
.next {
cursor: pointer;
position: absolute;
top: 50%;
width: auto;
margin-top: -22px;
padding: 16px;
color: black;
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;
margin-right: 0px;
}
/* On hover, add a black background color with a little bit see-through */
.prev:hover,
.next:hover {
background-color: rgba(0, 0, 0, 0.8);
}
/* Caption text */
.text {
color: #f2f2f2;
font-size: 50px;
padding: 8px 5px;
position: absolute;
bottom: 8px;
width: 100%;
text-align: center;
text-shadow: 2px 2px #000000;
}
/* 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;
margin-top: 660px;
}
.active,
.dot:hover {
background-color: #717171;
}
/* 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
}
}
.Succesverhalen_Sidebar {
float: right;
margin-left: 15px;
margin-top: 30px;
}
.Succesverhalen_Sidebar_text {
position: absolute;
margin-left: 1900px;
margin-top: 91px;
}
<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
<meta charset="utf-8">
<link rel="stylesheet" href="css/home.css">
<script src="JavaScript/home.js"></script>
<title>DHL Nieuws</title>
</head>
<header>
<img src="images/HeaderDHL.png" alt="header" class="headerimg">
<img src="images/banner-wave-3.svg" class="linkimg">
<meta name="viewport" content="width=device-width, initial-scale=1">
</header>
<body>
<div class="header">
Home
Rooster
Smoelenboek
Succesverhalen
</div>
<!-- Slideshow container -->
<div class="slideshow-container">
<!-- Full-width images with number and caption text -->
<div class="mySlides fade">
<img src="images/Sinterklaas_news.jpg" style="width:100%">
<div class="text">Sinterklaas is weer in het land!</div>
</div>
<div class="mySlides fade">
<img src="images/BlackFriday_news.jpg" style="width:100%">
<div class="text">Het is binnen kort Black Friday!</div>
</div>
<div class="mySlides fade">
<img src="images/kerst.jpg" style="width:100%">
<div class="text">Santa Claus is coming to town!</div>
</div>
<!-- Next and previous buttons -->
<a class="prev" onclick="plusSlides(-1)">❮</a>
<a class="next" onclick="plusSlides(1)">❯</a>
</div>
<br>
<!-- The dots/circles -->
<div>
<span class="dot" onclick="currentSlide(1)"></span>
<span class="dot" onclick="currentSlide(2)"></span>
<span class="dot" onclick="currentSlide(3)"></span>
</div>
<div class="Succesverhalen_Sidebar">
<img src="../images/Succesverhalen_Sidebar.png" style="margin-right:-279px;">
</div>
<div class="Succesverhalen_Sidebar_text">
<p>.........................<br>.........................<br>.........................
<br>.........................<br>......Lees verder</p>
<br>
<br>
<br>
<p>.........................<br>.........................<br>.........................
<br>.........................<br>......Lees verder</p>
<br>
<br>
<p>.........................<br>.........................<br>.........................
<br>.........................<br>......Lees verder</p>
<br>
<br>
<p>.........................<br>.........................<br>.........................
<br>.........................<br>......Lees verder</p>
<br>
<br>
<p>.........................<br>.........................<br>.........................
<br>.........................<br>......Lees verder</p>
<br>
<br>
<br>
<p>.........................<br>.........................<br>.........................
<br>.........................<br>......Lees verder</p>
<br>
<br>
<p>.........................<br>.........................<br>.........................
<br>.........................<br>......Lees verder</p>
</div>
</body>
</html>
I would like to receive an answer really fast because the deadline for the project ends in a few days. Thanks.
you just have to define slideIndex like var slideIndex=0; or =1 so it wil be var slideIndex = 1;
showSlides(slideIndex); and the javascript is not loading correctly because its in the head so the put the script tag at the bottom of body
There are some semantic issues in your code.
Taking for granted that that is your whole javascript code I see this problems:
showSlides(slideIndex); // --> Here you are calling showSlides function with slideIndex that is undefined,
// maybe you are missing something like "var slideIndex = 0" or similar
function plusSlides(n) {
showSlides(slideIndex += n);
}
function currentSlide(n) {
showSlides(slideIndex = n);
}
function showSlides(n) {
var i;
var slides = document.getElementsByClassName("mySlides");
var dots = document.getElementsByClassName("dot");
if (n > slides.length) {
slideIndex = 1 // --> here slideIndex is not yet declared, is it global?
// if it's not the case you have to declare it on top, like after "var i;" declaration.
}
if (n < 1) {
slideIndex = slides.length
}
for (i = 0; i < slides.length; i++) {
slides[i].style.display = "block";
}
for (i = 0; i < dots.length; i++) {
dots[i].className = dots[i].className.replace(" active", "");
}
slides[slideIndex - 1].style.display = "block";
dots[slideIndex - 1].className += "active";
}
fix these, give it a try and let me know
Last but not least if you want to load your javascript code inside the head tag you have to wrap it in a function that fires only after the DOM is fully loaded,
something like this:
document.addEventListener("DOMContentLoaded", function(event) {
//your code
});
Or eventually you can put your script tag immediately before the body closing tag

Vanilla JS left to right toggle animation

I am a newbie to code and I am learning vanilla JS. I created this drawer navigation with a circle that moves from left tor right, but there is an issue. I uploaded the current state here: http://setup.industries/masquarade/
The issue that gets me stuck:
The first click on the hamburger nav icon doesn't open the drawer and the animation is switched. I suspect the problem to be in the if(open) as the open var doesn't truly capture the toggle state with open = header.style.width == '0%' After the initial bug, it works fine. A real head scratcher for me.
If you have other tips for better code, or point out my bad practices, I'd be happy to learn.
--
Edit 1: I have added the full code to this question. I am not sure how I can make the ellipse visible, i linked directly to hosted url.
// open sidenav //
function openNav() {
let header = document.getElementById("header");
let open = header.style.width == '0%'
let width = document.body.clientWidth;
var ellipse = document.getElementById("ellipse");
function moveEllipseRight() {
ellipse.animate([
// keyframes
{ transform: 'translateX(0px)' },
{ transform: 'translateX('+ width + 'px)' }
], {
// timing options
duration: 500,
iterations: 1,
easing: 'ease-in-out',
fill: 'forwards'
});}
function moveEllipseLeft() {
ellipse.animate([
// keyframes
{ transform: 'translateX('+ width + 'px)' },
{ transform: 'translateX(0px)' }
], {
// timing options
duration: 500,
iterations: 1,
easing: 'ease-in-out',
fill: 'forwards'
});}
// open sidenav //
if (open) {
header.style.width = "100%";
moveEllipseRight();
} else {
header.style.width = '0%';
moveEllipseLeft();
}
}
// if (open) {
// ellipse.classList.remove("ellipse_left");
// ellipse.classList.add("ellipse_right");
// } else {
// ellipse.classList.remove("ellipse_right");
// ellipse.classList.add("ellipse_left");
// }
// let ellipse = document.getElementById("ellipse");
// let pos = 0;
// let id = setInterval(frame, 5);
// function myMove() {
// console.log('Hello')
// var ellipse = document.getElementById("ellipse");
// var pos = -200;
// var id = setInterval(frame, 1);
// let width = document.body.clientWidth; // - $('#mydiv').width();
//
// function frame() {
// if (pos == width - 200) {
// clearInterval(id);
// } else {
// pos++;
// ellipse.style.left = pos + "px";
// }
// }
// }
// information tabs //
function openTab(evt, tab) {
var i, tabcontent, tablinks;
tabcontent = document.getElementsByClassName("tabcontent");
for (i = 0; i < tabcontent.length; i++) {
tabcontent[i].style.display = "none";
}
tablinks = document.getElementsByClassName("tablinks");
for (i = 0; i < tablinks.length; i++) {
tablinks[i].className = tablinks[i].className.replace(" active", "");
}
document.getElementById(tab).style.display = "block";
evt.currentTarget.className += " active";
}
html, body {
max-width: 100%;
overflow-x: hidden;
}
body {
background: black;
color: white;
font-family: 'Helvetica Neue', sans-serif;
font-size: 1.2em;
line-height: 1.4em;
}
a {
color: white;
}
.clear {
clear: both; float: none; height: 40px;
}
/* Ellipse */
#ellipse {
position: absolute;
top: 120px;
z-index:99;
animation: 3s linear 0s slide 1;
left: -200px;
}
/*
.ellipse_left {left: -200px;}
.ellipse_right {right: -200px;}
*/
/* Masquarede Logo */
img.masquarade_events {
opacity: 0.3;
position: absolute;
bottom: 20px;
right: 20px;
}
img.masquarade_events:hover {
opacity: 0.9;
}
/* Content */
.content {
margin: 150px 0 0 300px;
width: 700px;
height: 400px;
}
#media screen and (max-width: 992px) {
.content {
margin: 150px 0 0 0;
width: 700px;
height: 400px;
}
}
.date {
font-weight: bold;
margin-bottom: -10px;
}
.location {
}
ul.lineup {
list-style-position: inside;
padding: 0;
list-style-type: none;
width: 100%
overflow: hidden;
margin-bottom: 100px;
}
ul.lineup li {
margin-right: 50px;
line-height: 2.5em;
float: left;
}
/* Buttons */
a.button {
margin-right: 10px;
padding: 10px 50px 10px 50px;
text-decoration: none;
border-radius: 200px;
font-size: 0.7em;
transition: 0.3s;
}
a.white {
background: white;
color: black;
}
a.white:hover {
color: white;
background: #D90E46;
}
a.black {
border: 2px white solid;
color: white;
}
a.black:hover {
border: 2px #FCF454 solid;
color: #FCF454;
}
/* Header */
header {
position: absolute;
background-color: black;
top:0;
left:0;
width: 0;
height: 100%;
z-index: 1;
}
/* Navigation */
header nav {
position: absolute;
top: 100px;
right:300px;
}
nav ul {
list-style-position: inside;
width: 400px;
padding: 0;
list-style-type: none;
font-size: 1em;
}
nav ul li{
border-bottom: 1px solid white;
padding: 10px 0 10px 0;
}
nav ul li:hover{
font-weight: bold;
padding: 10px 0 10px 10px;
}
li.active {
font-weight: bold;
}
nav ul li:first-child{
/* border-top: 1px solid white;*/
}
nav ul li a{
text-decoration: none;
}
nav ul h2{
margin-bottom: 10px;
}
.tabcontent {
display: none;
}
/* Header Icon */
img.icon {
position: absolute;
z-index: 999;
top:60px;
right:70px;
}
/* Display */
.display {
width: 400px;
height: 400px;
position: absolute;
top: 100px;
right:750px;
}
.display p {
margin:0 30px 30px 0;
}
<!doctype html>
<html lang="en">
<head>
<!-- SETUP Industries - FUNCTIONAL DESIGN -->
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<!-- <link rel="shortcut icon" type="image/png" href="favicon.png"/> -->
<!-- CSS -->
<link rel="stylesheet" type="text/css" href="assets/style.css" />
<link href="https://fonts.googleapis.com/css?family=Heebo:400,700,900" rel="stylesheet">
<!-- JS -->
<script src="assets/javascript.js"></script>
<title>Masquarade Classix 2019</title>
</head>
<body>
<!-- Navigation -->
<!-- Icon -->
<img onclick="openNav()"id="icon" src="http://setup.industries/masquarade/assets/icon.svg" class="icon" width="40" alt="Expand Navigation" />
<header id="header">
<nav>
<ul>
<h2>Information</h2>
<li class="tablinks" onmouseover="openTab(event, 'Tickets')">Tickets and pricing </li>
<li class="tablinks" onmouseover="openTab(event, 'Location')">Location</li>
<li class="tablinks" onmouseover="openTab(event, 'Transportation')">Transportation</li>
<li class="tablinks" onmouseover="openTab(event, 'Amenities')"><a href="#">Ameneties</li>
<li class="tablinks" onmouseover="openTab(event, 'HouseRules')">House rules</li>
<li class="tablinks" onmouseover="openTab(event, 'TermsAndConditions')">Terms and conditions</li>
<li class="tablinks" onmouseover="openTab(event, 'Contact')">Contact</li>
<li class="tablinks" onmouseover="openTab(event, 'Partners')">Partners</li>
</ul>
</nav>
<div class="display">
<div id="Tickets" class="tabcontent">
<h2>Tickets and pricing</h2>
<p>Saturday day tickets cost 45 EUR incl. service costs and 21% BTW. You can buy tickets online via the button below or at one of the resellers listed below.</p>
Buy Tickets
<br><br>
<p style="font-size:0.8em;"> <strong>Paperpoint</strong><br>
3930 Hamont-achel<br><br>
<strong>Dag en nachtwinkel </strong><br>
3900 Overpelt<br><br>
<strong>VDM bvba, Q8 tankstation</strong> <br>
Peer<br><br>
<strong>Frituur De Kromme Draai</strong> <br>
Eksel<br><br>
<strong>’t frituurke</strong> <br>
Haag 22, 3910 Achel<br></p>
</div>
<div id="Location" class="tabcontent">
<h2>Location</h2>
</div>
<div id="Transportation" class="tabcontent">
<h2>Transportation</h2>
</div>
<div id="Amenities" class="tabcontent">
<h2>Amenities</h2>
</div>
<div id="HouseRules" class="tabcontent">
<h2>House Rules</h2>
</div>
<div id="TermsAndConditions" class="tabcontent">
<h2>Terms And Conditions</h2>
</div>
<div id="Contact" class="tabcontent">
<h2>Contact</h2>
</div>
<div id="Partners" class="tabcontent">
<h2>Partners</h2>
</div>
</div>
</header>
<!-- Navigation End -->
<div class="container">
<div id="ellipse" class="ellipse_left">
<img src="assets/ellipse.svg" alt="ellipse" width="400" height="400"/>
</div>
<img class="masquarade_events" src="assets/masquarade_events.png" alt="Masquarade Events" width="125" height=""/>
<div class="content">
<p class="date">25 mei 2019</p>
<p class="location">Hennemeeuwis Neerpelt</p>
<h1>Masquarade Classix </h1>
<ul class="lineup">
<li>Nina Kraviz</li>
<li>Recondite</li>
<li>Mind Against</li>
<li>Âme</li>
<li>Vince Watson</li>
<li>Kölsch</li>
<li>Rodriguez Jr. </li>
<li></li>
</ul>
<div class="clear"></div>
Buy Tickets
More Information
</div>
</div>
</body>
</html>
The value for element.style is set by using either javascript or inline style attribute, css will not set the value for you. Therefore, if you are using a css to style the header's width, the value for header.style.width would be an empty string initially, making the expression header.style.width == '0%' to be falsy.
You can add a console.log(document.getElementById('header').style.width) to check the value yourself.
As a result, the first time you click the hamburger, the else block in the conditional will always be ran.
After the first time you click the hamburger, document.getElementById('header').style.width is now being set through javascript, so the subsequent clicks will behave as expected.
To solve the problem, you can either use an inline style attribute to style your header's width, or you can get the style using javascript with
const headerWidth = getComputedStyle(document.getElementById('header')).width;
const open = headerWidth === '0px' || headerWidth === '0%';
You had your open logic reversed. Simply changing the order of 100% / 0% fixes this.
Improvement tips:
checking the elements state by using a class instead of the actual CSS. (Adding an open class to the element when it is open, and removing it when it is not open)
By doing the above, you can move the manipulation of width to the CSS class open: header.open {width: 100%}
By using "`" and encapsulating your variables in ${} you can get rid of many "+" like so: { transform: `translateX(${width}px)` }
Can be seen in the snippet:
// open sidenav //
function openNav() {
let header = document.getElementById("header");
let open = header.className.includes('open')
let width = document.body.clientWidth;
var ellipse = document.getElementById("ellipse");
function moveEllipseRight() {
ellipse.animate([
// keyframes
{ transform: 'translateX(0)' },
{ transform: `translateX(${width}px)` }
], {
// timing options
duration: 500,
iterations: 1,
easing: 'ease-in-out',
fill: 'forwards'
});}
function moveEllipseLeft() {
ellipse.animate([
// keyframes
{ transform: `translateX(${width}px)` },
{ transform: 'translateX(0)' }
], {
// timing options
duration: 500,
iterations: 1,
easing: 'ease-in-out',
fill: 'forwards'
});}
// open sidenav //
if (open) {
moveEllipseLeft();
header.classList.remove("open");
} else {
moveEllipseRight();
header.classList.add("open");
}
}
// information tabs //
function openTab(evt, tab) {
var i, tabcontent, tablinks;
tabcontent = document.getElementsByClassName("tabcontent");
for (i = 0; i < tabcontent.length; i++) {
tabcontent[i].style.display = "none";
}
tablinks = document.getElementsByClassName("tablinks");
for (i = 0; i < tablinks.length; i++) {
tablinks[i].className = tablinks[i].className.replace(" active", "");
}
document.getElementById(tab).style.display = "block";
evt.currentTarget.className += " active";
}
html, body {
max-width: 100%;
overflow-x: hidden;
}
body {
background: black;
color: white;
font-family: 'Helvetica Neue', sans-serif;
font-size: 1.2em;
line-height: 1.4em;
}
a {
color: white;
}
.clear {
clear: both; float: none; height: 40px;
}
/* Ellipse */
#ellipse {
position: absolute;
top: 120px;
z-index:99;
animation: 3s linear 0s slide 1;
left: -200px;
}
/*
.ellipse_left {left: -200px;}
.ellipse_right {right: -200px;}
*/
/* Masquarede Logo */
img.masquarade_events {
opacity: 0.3;
position: absolute;
bottom: 20px;
right: 20px;
}
img.masquarade_events:hover {
opacity: 0.9;
}
/* Content */
.content {
margin: 150px 0 0 300px;
width: 700px;
height: 400px;
}
#media screen and (max-width: 992px) {
.content {
margin: 150px 0 0 0;
width: 700px;
height: 400px;
}
}
.date {
font-weight: bold;
margin-bottom: -10px;
}
.location {
}
ul.lineup {
list-style-position: inside;
padding: 0;
list-style-type: none;
width: 100%
overflow: hidden;
margin-bottom: 100px;
}
ul.lineup li {
margin-right: 50px;
line-height: 2.5em;
float: left;
}
/* Buttons */
a.button {
margin-right: 10px;
padding: 10px 50px 10px 50px;
text-decoration: none;
border-radius: 200px;
font-size: 0.7em;
transition: 0.3s;
}
a.white {
background: white;
color: black;
}
a.white:hover {
color: white;
background: #D90E46;
}
a.black {
border: 2px white solid;
color: white;
}
a.black:hover {
border: 2px #FCF454 solid;
color: #FCF454;
}
/* Header */
header {
position: absolute;
background-color: black;
top:0;
left:0;
width: 0;
height: 100%;
z-index: 1;
}
/* Header animation css */
header.open {
width: 100%;
}
/* Navigation */
header nav {
position: absolute;
top: 100px;
right:300px;
}
nav ul {
list-style-position: inside;
width: 400px;
padding: 0;
list-style-type: none;
font-size: 1em;
}
nav ul li{
border-bottom: 1px solid white;
padding: 10px 0 10px 0;
}
nav ul li:hover{
font-weight: bold;
padding: 10px 0 10px 10px;
}
li.active {
font-weight: bold;
}
nav ul li:first-child{
/* border-top: 1px solid white;*/
}
nav ul li a{
text-decoration: none;
}
nav ul h2{
margin-bottom: 10px;
}
.tabcontent {
display: none;
}
/* Header Icon */
img.icon {
position: absolute;
z-index: 999;
top:60px;
right:70px;
}
/* Display */
.display {
width: 400px;
height: 400px;
position: absolute;
top: 100px;
right:750px;
}
.display p {
margin:0 30px 30px 0;
}
<!doctype html>
<html lang="en">
<head>
<!-- SETUP Industries - FUNCTIONAL DESIGN -->
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<!-- <link rel="shortcut icon" type="image/png" href="favicon.png"/> -->
<!-- CSS -->
<link rel="stylesheet" type="text/css" href="assets/style.css" />
<link href="https://fonts.googleapis.com/css?family=Heebo:400,700,900" rel="stylesheet">
<!-- JS -->
<script src="assets/javascript.js"></script>
<title>Masquarade Classix 2019</title>
</head>
<body>
<!-- Navigation -->
<!-- Icon -->
<img onclick="openNav()"id="icon" src="http://setup.industries/masquarade/assets/icon.svg" class="icon" width="40" alt="Expand Navigation" />
<header id="header">
<nav>
<ul>
<h2>Information</h2>
<li class="tablinks" onmouseover="openTab(event, 'Tickets')">Tickets and pricing </li>
<li class="tablinks" onmouseover="openTab(event, 'Location')">Location</li>
<li class="tablinks" onmouseover="openTab(event, 'Transportation')">Transportation</li>
<li class="tablinks" onmouseover="openTab(event, 'Amenities')"><a href="#">Ameneties</li>
<li class="tablinks" onmouseover="openTab(event, 'HouseRules')">House rules</li>
<li class="tablinks" onmouseover="openTab(event, 'TermsAndConditions')">Terms and conditions</li>
<li class="tablinks" onmouseover="openTab(event, 'Contact')">Contact</li>
<li class="tablinks" onmouseover="openTab(event, 'Partners')">Partners</li>
</ul>
</nav>
<div class="display">
<div id="Tickets" class="tabcontent">
<h2>Tickets and pricing</h2>
<p>Saturday day tickets cost 45 EUR incl. service costs and 21% BTW. You can buy tickets online via the button below or at one of the resellers listed below.</p>
Buy Tickets
<br><br>
<p style="font-size:0.8em;"> <strong>Paperpoint</strong><br>
3930 Hamont-achel<br><br>
<strong>Dag en nachtwinkel </strong><br>
3900 Overpelt<br><br>
<strong>VDM bvba, Q8 tankstation</strong> <br>
Peer<br><br>
<strong>Frituur De Kromme Draai</strong> <br>
Eksel<br><br>
<strong>’t frituurke</strong> <br>
Haag 22, 3910 Achel<br></p>
</div>
<div id="Location" class="tabcontent">
<h2>Location</h2>
</div>
<div id="Transportation" class="tabcontent">
<h2>Transportation</h2>
</div>
<div id="Amenities" class="tabcontent">
<h2>Amenities</h2>
</div>
<div id="HouseRules" class="tabcontent">
<h2>House Rules</h2>
</div>
<div id="TermsAndConditions" class="tabcontent">
<h2>Terms And Conditions</h2>
</div>
<div id="Contact" class="tabcontent">
<h2>Contact</h2>
</div>
<div id="Partners" class="tabcontent">
<h2>Partners</h2>
</div>
</div>
</header>
<!-- Navigation End -->
<div class="container">
<div id="ellipse" class="ellipse_left">
<img src="assets/ellipse.svg" alt="ellipse" width="400" height="400"/>
</div>
<img class="masquarade_events" src="assets/masquarade_events.png" alt="Masquarade Events" width="125" height=""/>
<div class="content">
<p class="date">25 mei 2019</p>
<p class="location">Hennemeeuwis Neerpelt</p>
<h1>Masquarade Classix </h1>
<ul class="lineup">
<li>Nina Kraviz</li>
<li>Recondite</li>
<li>Mind Against</li>
<li>Âme</li>
<li>Vince Watson</li>
<li>Kölsch</li>
<li>Rodriguez Jr. </li>
<li></li>
</ul>
<div class="clear"></div>
Buy Tickets
More Information
</div>
</div>
</body>
</html>

Slider & Light Box - Javascript conflict

Would you anyone please help resolve this conflict? I would really appreciate that.
I am trying to use two javascripts of Slider and Light Box from w3schools.com on one HTML page but, I guess, both javascripts use the same names(or values) for each function so when I use them in one page at the same time, they don't work. but when I remove one set of css/html/javascript for each slider or light box, either works well.
Please help this for me.
Thank you very much in advance.
CSS
<style>
body {
font-family: Verdana, sans-serif;
margin: 0;
}
/* --------- Slider ---------- */
* {box-sizing:border-box}
/* Slideshow container */
.slideshow-container {
max-width: 1000px;
position: relative;
margin: auto;
}
.mySlides {
display: none;
}
/* Next & previous buttons */
.prev, .next {
cursor: pointer;
position: absolute;
top: 50%;
width: auto;
margin-top: -22px;
padding: 16px;
color: white;
font-weight: bold;
font-size: 18px;
transition: 0.6s ease;
border-radius: 0 3px 3px 0;
}
/* Position the "next button" to the right */
.next {
right: 0;
border-radius: 3px 0 0 3px;
}
/* On hover, add a black background color with a little bit see-through */
.prev:hover, .next:hover {
background-color: rgba(0,0,0,0.8);
}
/* Caption text */
.text {
color: #f2f2f2;
font-size: 15px;
padding: 8px 12px;
position: absolute;
bottom: 8px;
width: 100%;
text-align: center;
}
/* Number text (1/3 etc) */
.numbertext {
color: #f2f2f2;
font-size: 12px;
padding: 8px 12px;
position: absolute;
top: 0;
}
/* The dots/bullets/indicators */
.dot {
cursor:pointer;
height: 13px;
width: 13px;
margin: 0 2px;
background-color: #bbb;
border-radius: 50%;
display: inline-block;
transition: background-color 0.6s ease;
}
.active, .dot:hover {
background-color: #717171;
}
/* 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}
}
/* ----- Light Box -------*/
.row > .column {
padding: 0 8px;
}
.row:after {
content: "";
display: table;
clear: both;
}
.column {
float: left;
width: 25%;
}
/* The Modal (background) */
.modal {
display: none;
position: fixed;
z-index: 1;
padding-top: 100px;
left: 0;
top: 0;
width: 100%;
height: 100%;
overflow: auto;
background-color: black;
}
/* Modal Content */
.modal-content {
position: relative;
background-color: transparent;
margin: auto;
padding: 0;
width: 90%;
max-width: 1200px;
border: none;
}
/* The Close Button */
.close {
color: white;
position: absolute;
top: 10px;
right: 25px;
font-size: 35px;
font-weight: bold;
}
.close:hover,
.close:focus {
color: #999;
text-decoration: none;
cursor: pointer;
}
.mySlides {
display: none;
}
.cursor {
cursor: pointer
}
/* Next & previous buttons */
.prev,
.next {
cursor: pointer;
position: absolute;
top: 50%;
width: auto;
padding: 16px;
margin-top: -50px;
color: white;
font-weight: bold;
font-size: 20px;
transition: 0.6s ease;
border-radius: 0 3px 3px 0;
user-select: none;
-webkit-user-select: none;
}
/* Position the "next button" to the right */
.next {
right: 0;
border-radius: 3px 0 0 3px;
}
/* On hover, add a black background color with a little bit see-through */
.prev:hover,
.next:hover {
background-color: rgba(0, 0, 0, 0.8);
}
/* Number text (1/3 etc) */
.numbertext {
color: #f2f2f2;
font-size: 12px;
padding: 8px 12px;
position: absolute;
top: 0;
}
img {
margin-bottom: -4px;
}
.caption-container {
text-align: center;
background-color: black;
padding: 2px 16px;
color: white;
}
.demo {
opacity: 0.6;
}
.active,
.demo:hover {
opacity: 1;
}
img.hover-shadow {
transition: 0.3s
}
.hover-shadow:hover {
box-shadow: 0 4px 8px 0 rgba(0, 0, 0, 0.2), 0 6px 20px 0 rgba(0, 0, 0, 0.19)
}
.container {margin: 0 auto; max-width: 1000px;}
</style>
HTML
<body>
<div class="container">
<!-- ================ Slideshow ================ -->
<div class="slideshow-container mt20 mb10">
<div class="mySlides fade">
<div class="numbertext">1 / 3</div>
<img src="https://www.w3schools.com/howto/img_nature_wide.jpg" style="width:100%">
<div class="text">Caption Text</div>
</div>
<div class="mySlides fade">
<div class="numbertext">2 / 3</div>
<img src="https://www.w3schools.com/howto/img_fjords_wide.jpg" style="width:100%">
<div class="text">Caption Two</div>
</div>
<div class="mySlides fade">
<div class="numbertext">3 / 3</div>
<img src="https://www.w3schools.com/howto/img_mountains_wide.jpg" style="width:100%">
<div class="text">Caption Three</div>
</div>
<a class="prev" onclick="plusSlides(-1)">❮</a>
<a class="next" onclick="plusSlides(1)">❯</a>
</div>
<div style="text-align:center;margin-top:10px;">
<span class="dot" onclick="currentSlide(1)"></span>
<span class="dot" onclick="currentSlide(2)"></span>
<span class="dot" onclick="currentSlide(3)"></span>
</div> <!-- // Slider -->
<!-- Lightbox -->
<h2 style="text-align:center">Lightbox</h2>
<div class="row">
<div class="column">
<img src="https://www.w3schools.com/howto/img_nature.jpg" style="width:100%" onclick="openModal();currentSlide(1)" class="hover-shadow cursor">
</div>
<div class="column">
<img src="https://www.w3schools.com/howto/img_fjords.jpg" style="width:100%" onclick="openModal();currentSlide(2)" class="hover-shadow cursor">
</div>
<div class="column">
<img src="https://www.w3schools.com/howto/img_mountains.jpg" style="width:100%" onclick="openModal();currentSlide(3)" class="hover-shadow cursor">
</div>
<div class="column">
<img src="https://www.w3schools.com/howto/img_lights.jpg" style="width:100%" onclick="openModal();currentSlide(4)" class="hover-shadow cursor">
</div>
</div>
<div id="myLightBox" class="modal">
<span class="close cursor" onclick="closeModal()">×</span>
<div class="modal-content">
<div class="LB-Slides">
<div class="numbertext">1 / 4</div>
<img src="https://www.w3schools.com/howto/img_nature_wide.jpg" style="width:100%">
</div>
<div class="LB-Slides">
<div class="numbertext">2 / 4</div>
<img src="https://www.w3schools.com/howto/img_fjords_wide.jpg" style="width:100%">
</div>
<div class="LB-Slides">
<div class="numbertext">3 / 4</div>
<img src="https://www.w3schools.com/howto/img_mountains_wide.jpg" style="width:100%">
</div>
<div class="LB-Slides">
<div class="numbertext">4 / 4</div>
<img src="https://www.w3schools.com/howto/img_lights_wide.jpg" style="width:100%">
</div>
<a class="prev" onclick="plusSlides(-1)">❮</a>
<a class="next" onclick="plusSlides(1)">❯</a>
<div class="caption-container">
<p id="caption"></p>
</div>
<div class="column">
<img class="demo cursor" src="https://www.w3schools.com/howto/img_nature_wide.jpg" style="width:100%" onclick="currentSlide(1)" alt="Nature and sunrise">
</div>
<div class="column">
<img class="demo cursor" src="https://www.w3schools.com/howto/img_fjords_wide.jpg" style="width:100%" onclick="currentSlide(2)" alt="Trolltunga, Norway">
</div>
<div class="column">
<img class="demo cursor" src="https://www.w3schools.com/howto/img_mountains_wide.jpg" style="width:100%" onclick="currentSlide(3)" alt="Mountains and fjords">
</div>
<div class="column">
<img class="demo cursor" src="https://www.w3schools.com/howto/img_lights_wide.jpg" style="width:100%" onclick="currentSlide(4)" alt="Northern Lights">
</div>
</div>
</div>
</div><!-- // Lightbox -->
</div>
Javascript
<script>
/* --------- Slider ---------- */
var slideIndex = 1;
showSlides(slideIndex);
setInterval(function(){ showSlides(++slideIndex); }, 4000);
function plusSlides(n) {
showSlides(slideIndex += n);
}
function currentSlide(n) {
showSlides(slideIndex = n);
}
function showSlides(n) {
var i;
var slides = document.getElementsByClassName("mySlides");
var dots = document.getElementsByClassName("dot");
if (n > slides.length) {slideIndex = 1}
if (n < 1) {slideIndex = slides.length}
for (i = 0; i < slides.length; i++) {
slides[i].style.display = "none";
}
for (i = 0; i < dots.length; i++) {
dots[i].className = dots[i].className.replace(" active", "");
}
slides[slideIndex-1].style.display = "block";
dots[slideIndex-1].className += " active";
}
/* --------- LightBox ---------- */
function openModal() {
document.getElementById('myLightBox').style.display = "block";
}
function closeModal() {
document.getElementById('myLightBox').style.display = "none";
}
var slideIndex = 1;
showSlides(slideIndex);
function plusSlides(n) {
showSlides(slideIndex += n);
}
function currentSlide(n) {
showSlides(slideIndex = n);
}
function showSlides(n) {
var i;
var slides = document.getElementsByClassName("LB-Slides");
var dots = document.getElementsByClassName("demo");
var captionText = document.getElementById("caption");
if (n > slides.length) {slideIndex = 1}
if (n < 1) {slideIndex = slides.length}
for (i = 0; i < slides.length; i++) {
slides[i].style.display = "none";
}
for (i = 0; i < dots.length; i++) {
dots[i].className = dots[i].className.replace(" active", "");
}
slides[slideIndex-1].style.display = "block";
dots[slideIndex-1].className += " active";
captionText.innerHTML = dots[slideIndex-1].alt;
}
/* --------- //LightBox ---------- */
</script>
</body>
First of all – yes, you are right, both of your scripts are conflicting to each other due the same methods names. Since it's written in global scope (window) you basically override each other functionality.
You could try to use JavaScript module pattern to isolate scope of each of your "widgets", even if you gonna use the same methods names, your code will be executed in it's own scope.
As quick example how to rewrite this into modules:
http://jsbin.com/pipajawiwa/1/edit?html,js,output
/* --------- Slider ---------- */
var slider = (function() {
var slider = {};
slider.setIntervals = function() {
setInterval(function() {
slider.showSlides(++slider.slideIndex);
}, 4000);
};
slider.plusSlides = function(n) {
slider.showSlides(slider.slideIndex += n);
};
slider.currentSlide = function(n) {
slider.showSlides(slider.slideIndex = n);
};
slider.showSlides = function(n) {
var i;
var slides = document.getElementsByClassName("mySlides");
var dots = document.getElementsByClassName("dot");
if (n > slides.length) {
slider.slideIndex = 1
}
if (n < 1) {
slider.slideIndex = slides.length
}
for (i = 0; i < slides.length; i++) {
slides[i].style.display = "none";
}
for (i = 0; i < dots.length; i++) {
dots[i].className = dots[i].className.replace(" active", "");
}
slides[slider.slideIndex - 1].style.display = "block";
dots[slider.slideIndex - 1].className += " active";
};
slider.init = function() {
slider.slideIndex = 1;
slider.showSlides(slider.slideIndex);
slider.setIntervals();
};
return slider;
})();
/* --------- LightBox ---------- */
var lightbox = (function() {
var lightbox = {};
lightbox.openModal = function() {
document.getElementById('myLightBox').style.display = "block";
};
lightbox.closeModal = function() {
document.getElementById('myLightBox').style.display = "none";
};
lightbox.plusSlides = function(n) {
lightbox.showSlides(lightbox.slideIndex += n);
};
lightbox.currentSlide = function(n) {
lightbox.showSlides(lightbox.slideIndex = n);
};
lightbox.showSlides = function(n) {
var i;
var slides = document.getElementsByClassName("LB-Slides");
var dots = document.getElementsByClassName("demo");
var captionText = document.getElementById("caption");
if (n > slides.length) {
lightbox.slideIndex = 1
}
if (n < 1) {
lightbox.slideIndex = slides.length
}
for (i = 0; i < slides.length; i++) {
slides[i].style.display = "none";
}
for (i = 0; i < dots.length; i++) {
dots[i].className = dots[i].className.replace(" active", "");
}
slides[lightbox.slideIndex - 1].style.display = "block";
dots[lightbox.slideIndex - 1].className += " active";
captionText.innerHTML = dots[lightbox.slideIndex - 1].alt;
};
lightbox.init = function() {
lightbox.slideIndex = 1;
lightbox.showSlides(lightbox.slideIndex);
};
return lightbox;
})();
slider.init(); // initialize slider module (widget);
lightbox.init(); // initialize lightbox module (widget);
Please make note that you also need to adjust your html onclick events, since it's should be called each widget methods, not global once.
P.S.: I'm not big fan of both of this code snippets from w3school (in terms of code quality, performance aspects, learning patterns), but it's completely another topic to discuss. But in order "to make it work" this link to jsbin above should help.

Categories