Several slideshows on one page - javascript

I have read threads asking the same question but I can not find information thats relevant for the functions I am using
I have made an automatic slideshow in HTML CSS and JS. This works perfectly, but it does not work on two slideshows at the same time.
The JS: I tried a couple of different things, lastly I tried to make two independent functions for the two slideshows. This does not work either
//Necessary variables
var images = [];
var i = 0;
var time = 2000;
// List of pics
images[0] = "images/nighttime.png";
images[1] = "images/natur.png";
images[2] = "images/shark_1.png";
images[3] = "images/shark_2.jpg";
function autoSlide () {
document.slide.src = images[i];
if (i < images.length-1) {
i++;
}
else {
i = 0;
}
setTimeout("autoSlide()", time);
}
window.onload = autoSlide;
/* Slideshow 2 */
// Necessary variables
var images_one = [];
var j = 0;
// List of pics
images_one[0] = "images/innredning_1.png";
images_one[1] = "images/innredning_2.png";
images_one[2] = "images/innredning_bad.jpg";
function autoSlide_box2 () {
document.slide.src = images_one[i];
if (j < images_one.length-1) {
j++;
}
else {
j = 0;
}
setTimeout("autoSlide()_box2", time);
}
window.onload = autoSlide_box2;
The HTML: The overlay__content is for the function openNav(), it should not have anything to do with the slideshow function
<section class="index-content">
<!-- Denne containeren viser frem bildene. Skifter automatisk-->
<div class="container">
<figure id="slideshow" onclick="openNav()">
<img src="images/nighttime.png" name="slide" class="pictures">
</figure>
</div>
<!-- Denne div'en og alt inni den er usynlig. Kommer frem når man trykker på bildet. Forhåpentligvis-->
<div id="nav" class="overlay">
<div class="overlay__content1">
×
<div class="mySlides fade">
<img src="images/nighttime.png">
</div>
<div class="mySlides fade">
<img src="images/natur.png">
</div>
<div class="mySlides fade">
<img src="images/shark_1.png">
</div>
<div class="mySlides fade" id="threenfour">
<img src="images/shark_2.jpg">
</div>
</div>
</div>
</section>
<section class="index-content2">
<div class="containerLeft">
<figure id="slideshow" onclick="openNav()">
<img src="images/innredning_1.png" name="slide" class="pictures">
</figure>
</div>
<!-- Denne div'en og alt inni den er usynlig. Kommer frem når man trykker på bildet. Forhåpentligvis-->
<div id="nav" class="overlay">
<div class="overlay__content2">
<div class="mySlides fade">
<img src="images/innredning_1.png">
</div>
<div class="mySlides fade">
<img src="images/innredning_2.png">
</div>
<div class="mySlides fade">
<img src="images/innredning_bad.jpgw">
</div>
</div>
</div>
</section>

I don't know exactly, but i think you are adressing the same slide within your functions:
document.slide.src = images[i];
and
document.slide.src = images_one[i];
maybe you need to try to give your images ids, like proposed in this answer by NewToJS

I found the answer. I had to change the function so it would change the pictures of every slideshow on the page.
function autoSlide () {
// Sliding the images
document.slide.src = images[i];
document.slide1.src = images_one[j];
document.slide2.src = images_two[k];
// Variables reset after going through the array
if (i < images.length-1) {i++;}
else {i = 0;}
if (j < images_one.length-1) {j++;}
else {j = 0;}
if (k < images_two.length-1) {k++;}
else {k = 0;}
setTimeout("autoSlide()", time);
}

Related

Trouble with JavaScript based photo gallery

I'm attempting to adapt the HTML, CSS, an JavaScript from here: https://www.w3schools.com/howto/howto_js_slideshow_gallery.asp
The problem I'm having is I would like to have all the JavaScript in the JS file and none be in the html.
When I try to move the onclick out of the HTML and into the JS file I have had nothing but problems. Beginning with the DOM not being loaded(solved with a domLoaded function) then to a problem with event listeners for the individual pictures.
My current problem is that the slides in the showSlides function is undefined if I pass the number 1 through it, either as you see in the document.querySelector or if I call the currentSlides or showSlides function in the console.
//waits for the DOM to load so that everything is ready for the gallery code.
window.addEventListener("DOMContentLoaded", domLoaded);
function domLoaded() {
showSlides(slideIndex);
}
document.querySelector("#img1").addEventListener("click", currentSlide.bind(1));
document.querySelector("#img2").addEventListener("click", currentSlide.bind(2));
document.querySelector("#img3").addEventListener("click", currentSlide.bind(3));
document.querySelector('#img4').addEventListener("click", currentSlide.bind(4));
document.querySelector('#img5').addEventListener("click", currentSlide.bind(5));
document.querySelector('#img6').addEventListener("click", currentSlide.bind(6));
//Begin w3schools gallery code
var slideIndex = 1;
// Next/previous controls
function plusSlides(n) {
showSlides(slideIndex += n);
}
// Thumbnail image controls
function currentSlide(n) {
showSlides(slideIndex = n);
}
function showSlides(n) {
console.log("click working")
let i;
var slides = document.getElementsByClassName("mySlides");
let dots = document.getElementsByClassName("demo");
let 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;
}
<div class="container">
<!-- Full-width images with number text -->
<div class="mySlides">
<div class="numbertext">1 / 6</div>
<img class="mainImage" src="assets/dancingSnow.jpg">
</div>
<div class="mySlides">
<div class="numbertext">2 / 6</div>
<img class="mainImage" src="assets/CABELL CO TOURNEY 2016.jpg">
</div>
<div class="mySlides">
<div class="numbertext">3 / 6</div>
<img class="mainImage" src="assets/IMG_9320.JPG">
</div>
<div class="mySlides">
<div class="numbertext">4 / 6</div>
<img class="mainImage" src="assets/Invitation.jpg">
</div>
<div class="mySlides">
<div class="numbertext">5 / 6</div>
<img class="mainImage" src="assets/SCAbattleline.jpg">
</div>
<div class="mySlides">
<div class="numbertext">6 / 6</div>
<img class="mainImage" src="assets/Yorick Paragon Druid.jpg">
</div>
<!-- Next and previous buttons -->
<a class="prev" onclick="plusSlides(-1)">❮</a>
<a class="next" onclick="plusSlides(1)">❯</a>
<!-- Image text -->
<div class="caption-container">
<p id="caption"></p>
</div>
<!-- Thumbnail images -->
<div class="row">
<div class="column">
<img class="demo" src="assets/dancingSnow.jpg" id="img1" alt="Dancing in the Snow">
</div>
<div class="column">
<img class="demo" src="assets/CABELL CO TOURNEY 2016.jpg" id="img2" alt="Cabell Co Tourney 2016">
</div>
<div class="column">
<img class="demo" src="assets/IMG_9320.JPG" id="img3" alt="unknown">
</div>
<div class="column">
<img class="demo" src="assets/Invitation.jpg" id="img4" alt="Invitation">
</div>
<div class="column">
<img class="demo" src="assets/SCAbattleline.jpg" id="img5" alt="Ready for War">
</div>
<div class="column">
<img class="demo" src="assets/Yorick Paragon Druid.jpg" id="img6" alt="Amtgard Award">
</div>
</div>
</div>
Any input is much appreciated.
With your bind solution, plusSlides function works incorrectly. If you put console.log(n) inside you'll see that n is not a number but rather [object PointerEvent], and so your slideIndex gets messed up and breaks showSlides function. The first argument of bind is usually the this pointer and you have to offset your desired arguments by one. You can pass null as the first argument, like this:
document.querySelector("#img1").addEventListener("click", currentSlide.bind(null,1));
Here is a working example:
https://jsfiddle.net/progmars/njwo0ct7/
However, it could be so much cleaner with jQuery and better model separation from view.

How can I reset my html image gallery to start with first image after leaving it?

I have a website with multiple image galleries on different parts of the website. When you click on the image of specific gallery it changes to next one of that gallery and so on.
What I am trying to achieve is to reset the previous gallery to image 1 when you start clicking on a different gallery. So when the user goes back to the previous gallery, it would start from the first image.
Code used for the galleries:
let projectIndexes = {
project1: 1,
project2: 1,
}
showDivs("project1", projectIndexes.project1);
showDivs("project2", projectIndexes.project2);
function plusDivs(project, n) {
showDivs(project, projectIndexes[project] += n);
}
function showDivs(project, index) {
let i;
let x = document.getElementById(project).getElementsByClassName("slidess");
if (index > x.length) { index = 1 }
if (index < 1) { index = x.length }
for (i = 0; i < x.length; i++) {
x[i].style.display = "none";
}
x[index - 1].style.display = "block";
projectIndexes[project] = index;
let elements = document.getElementById(project).querySelector('.imgslide').children;
let imgNames = [];
for (let i = 0; i < elements.length; i++) {
imgNames.push(elements[i].children[0].children[0].alt);
}
}
<div class="slide">
<div class="slide-content">
<div class="nextt" onclick="plusDivs('project1', 1)"></div>
</div>
<div class="image-container container prjct">
<div class="projects" id="project1">
<div class="imgslide noselect">
<div class="content-container slidess">
<div class="style-3 style-3-left">
<img class="imageName" alt="Img" src="">
</div>
<div class="style-3 style-3-middle">
<img class="imageName" alt="Img" src="">
</div>
<div class="style-3 style-3-right">
<img class="imageName" alt="Img" src="">
</div>
</div>
</div>
</div>
</div>
</div>
<div class="slide">
<div class="slide-content">
<div class="nextt" onclick="plusDivs('project2', 1)"></div>
</div>
<div class="image-container container prjct">
<div class="projects" id="project2">
<div class="imgslide noselect">
<div class="content-container slidess">
<div class="style-3 style-3-left">
<img class="imageName" alt="Img" src="">
</div>
<div class="style-3 style-3-middle">
<img class="imageName" alt="Img" src="">
</div>
<div class="style-3 style-3-right">
<img class="imageName" alt="Img" src="">
</div>
</div>
<!-- <div class="img-name"></div> -->
</div>
</div>
</div>
</div>
I know a way to force show the first element, but it turns out it does that for all projects.
What I was not able to find is a way to recognise when clicking on a new project, that the previous and only previous project needs to reset to first image.
I have been struggling with this for a while now and cannot make it work, so any help would be highly appreciated. And if something is not clear, let me know and I will clarify things.
If I'm following you correctly, the following should do it.
function resetPriorGalleries(currentProject) {
var projectDivs = document.getElementsByClassName("projects");
for (let i = 0; i < projectDivs.length; i++) {
if (projectDivs[i].id === currentProject)
return;
showDivs(projectDivs[1].id, 1);
}
}
The best place to call it would probably be in the onclicks.
onclick="plusDivs('project2', 1); resetPriorGalleries('project2');"

Automatic Cycling Carousel for several divs and different img

I am trying to write a function, that will automatically carousel cycle through img thumbs on my site with 12 different sets of images, which are just put together in one div. The javascript below works, but only as long, as I have the same amount of images in every div. I am also sure, that I took the long route of telling javascript, what to do in terms of variables so my question is, what should I change, so that I can have different amounts of img in my separate divs?
Thanks a lot for any tips!
var myIndex = 0;
carousel();
function carousel() {
var i;
var u = document.getElementsByClassName("thumbs1");
var v = document.getElementsByClassName("thumbs2");
var w = document.getElementsByClassName("thumbs3");
// and so on ...
for (i = 0; i < w.length; i++) {
u[i].style.display = "none";
v[i].style.display = "none";
w[i].style.display = "none";
// ...
}
myIndex++;
if (myIndex > w.length) {myIndex = 1}
u[myIndex-1].style.display = "inline-block";
v[myIndex-1].style.display = "inline-block";
w[myIndex-1].style.display = "inline-block";
// ...
setTimeout(carousel, 1200); // Change image every 2 seconds
}
<div class="imageholder">
<img class="thumbs1" src="image11.jpg">
<img class="thumbs1" src="image12.jpg">
<img class="thumbs1" src="image13.jpg">
<img class="thumbs1" src="image14.jpg">
</div>
<div class="imageholder">
<img class="thumbs2" src="image21.jpg">
<img class="thumbs2" src="image22.jpg">
<img class="thumbs2" src="image23.jpg">
</div>
<div class="imageholder">
<img class="thumbs3" src="image31.jpg">
<img class="thumbs3" src="image32.jpg">
<img class="thumbs3" src="image33.jpg">
<img class="thumbs3" src="image34.jpg">
<img class="thumbs3" src="image35.jpg">
</div>
<!-- ... -->
Keep counting the myIndex (so do not reset it) and use the modulus % operator with the array length of each image set.
Further notes:
The value of Timeout/Interval is in milliseconds. So 2000ms = 2s
You do not need to get all elements' references again with each call to carousel(). Just do it once at the beginning.
If something is not clear, please ask.
var myIndex = 0;
var i;
var u = document.getElementsByClassName("thumbs1");
var v = document.getElementsByClassName("thumbs2");
var w = document.getElementsByClassName("thumbs3");
var allThumbs = [u, v, w];
var myInterval = setInterval(carousel, 2000); // Change image every 2 seconds
function carousel() {
myIndex++;
for (i = 0; i < allThumbs.length; i++) {
allThumbs[i][(myIndex - 1) % allThumbs[i].length].style.display = "none";
allThumbs[i][myIndex % allThumbs[i].length].style.display = "inline-block";
}
}
.thumbs1:not(:first-child),
.thumbs2:not(:first-child),
.thumbs3:not(:first-child) {
display: none;
}
<div class="imageholder">
<img class="thumbs1" src="image11.jpg" alt="1">
<img class="thumbs1" src="image12.jpg" alt="2">
<img class="thumbs1" src="image13.jpg" alt="3">
<img class="thumbs1" src="image14.jpg" alt="4">
</div>
<div class="imageholder">
<img class="thumbs2" src="image21.jpg" alt="1">
<img class="thumbs2" src="image22.jpg" alt="2">
<img class="thumbs2" src="image23.jpg" alt="3">
</div>
<div class="imageholder">
<img class="thumbs3" src="image31.jpg" alt="1">
<img class="thumbs3" src="image32.jpg" alt="2">
<img class="thumbs3" src="image33.jpg" alt="3">
<img class="thumbs3" src="image34.jpg" alt="4">
<img class="thumbs3" src="image35.jpg" alt="5">
</div>

Carousel not displaying image

I have a carousel that is not displaying the images upon loading the webpage for some reason. I am working in dream weaver and for some reason the JS function only kicks in after clicking on one of the arrow buttons. I feel like I need to force feed the function one image in order for it show upon loading the page but I am not sure how to do this. Thanks for the help!
Here is the HTML (I have the JS fucntion in the head tags):
var slideIndex = 1;
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 = "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";
}
<script src="testcar.js"></script>
<section class="carousel">
<div class="slideshow-container">
<!-- Full-width images with number and caption text -->
<div class="mySlides fade">
<div class="numbertext">1 / 3</div>
<img src="https://www.healthypawspetinsurance.com/Images/V3/DogAndPuppyInsurance/Dog_CTA_Desktop_HeroImage.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.cesarsway.com/sites/newcesarsway/files/styles/large_article_preview/public/Common-dog-behaviors-explained.jpg?itok=FSzwbBoi" style="width:100%">
<div class="text">Caption Two</div>
</div>
<div class="mySlides fade">
<div class="numbertext">3 / 3</div>
<img src="https://www.what-dog.net/Images/faces2/scroll001.jpg" style="width:80%">
<div class="text">Caption Three</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 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>
</section>
Just try loading your JS after the HTML.
Check out the difference here:
https://jsfiddle.net/vy9uwxhk/ - look at the console for the error. As it's not finding the appropriate HTML, the code execution stops and results in non-working of the carousel.
https://jsfiddle.net/6k92zvze/ - here the <script> is loaded after the HTML and as it finds the HTML, the execution is successful and the carousel works fine.
Hope this helps. Here's more info on where to include your script tags.

How to get the refrence of the current image in image slider-javascript?

I want to get the detail of the image element which is currently previewed to user when they are navigating to the images with the slider.
slider.html:-
<div class="pop-box">
<div class="slideshow-container">
<div class="mySlides2 fade">
<img src="img/challenge-image.jpg">
<div class="text">Caption Text</div>
</div>
<div class="mySlides2 fade">
<img src="img/challenger-participation.jpg">
<div class="text">Caption Two</div>
</div>
<div class="mySlides2 fade">
<img src="img/banner.jpg">
<div class="text">Caption Three</div>
</div>
<div class="mySlides2 fade">
<img src="img/img-1.jpg">
<div class="text">Caption Three</div>
</div>
<div class="mySlides2 fade">
<img src="img/img-2.jpg">
<div class="text">Caption Three</div>
</div>
<div class="mySlides2 fade">
<img src="img/img-3.jpg">
<div class="text">Caption Three</div>
</div>
<div class="mySlides2 fade">
<img src="img/people-first.jpg">
<div class="text">Caption Three</div>
</div>
<a class="prev" onclick="plusSlides2(-1)">❮</a>
<a class="next" onclick="plusSlides2(1)">❯</a>
</div>
</div>
So what i want is when users click prev or next button then they will get a preview of some image from the above list i just want to get the detail of that image element on every instant of click.
My slider.js:-
var slideIndex = 1;
showSlides2(slideIndex);
function plusSlides2(n) {
showSlides2(slideIndex += n);
}
function currentSlide2(n) {
showSlides2(slideIndex = n);
}
function showSlides2(n) {
var i;
var slides = document.getElementsByClassName("mySlides2");
var dots = document.getElementsByClassName("dot2");
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 = "flex";
dots[slideIndex-1].className += " active";
}
Actually I found that I have half solution in my question I just need to add this piece of code in my slider.js
var ImgElement = slides[slideIndex-1].children[0];
this Will give me the exact image element which I want.Since my slider.js:-
var slides = document.getElementsByClassName("mySlides2");
if (n > slides.length) {slideIndex = 1}
if (n < 1) {slideIndex = slides.length}
for (i = 0; i < slides.length; i++) {
slides[i].style.display = "none";
}
slides[slideIndex-1].style.display = "flex";
So in my actual html code i have different images which is under class myslides2 ,and what i have done in my slider.js is i have added style :"none" while iterating over my images and after the loop ends i added stye:"flex" to the last div,and this is my target div which contain my desired image so finally i have done this by getting its childeren which is the first element of array.since getting any element by classname returns a list,so that's why i have done in this way.

Categories