I’m currently building a website with Bootstrap 4 and I want to build a function which allows me to show the next img in a list by clicking on the main-img or "expanded-img" in my code.
I already made a function to display the img by clicking on thumbnails, but I had some troubles, probably because the two functions interrupted each other.
You can see here that i have left below the sidebar 3 thumbnails.
When i click on a thumbnail the img will be shown in the div for the "expandedImg".
I did it with the following code:
function expandingImage(imgs) {
var expandImg = document.getElementById("expandedImg");
var imgText = document.getElementById("imgtext");
expandImg.src = imgs.src;
imgText.innerHTML = imgs.alt;
expandImg.parentElement.style.display = "block";
}
<!--START Thumbnaill Navbar-->
<div class="thumbnailContainer">
<img src="#####" alt="THIS IS TITLE NR1" class="img-thumbnail border-0 img-thumbnail-desktop" onclick="expandingImage(this);">
<img src="#####" alt="THIS IS TITLE NR2" class="img-thumbnail border-0 img-thumbnail-desktop" onclick="expandingImage(this);">
<img src="#####" alt="THIS IS TITLE NR3" class="img-thumbnail border-0 img-thumbnail-desktop" onclick="expandingImage(this);">
</div>
<!--END Thumbnaill Navbar-->
<div>
<div class="col-md-9 ">
<!--START MAIN CONTENT-->
<div class="hideMobile">
<div class="d-flex justify-content-center">
<span onclick="this.parentElement.style.display='none'"></span>
<img class="img-fluid img-overlay" id="expandedImg" style="max-height: 80vh;" src="#####">
</div>
<div class="container expandedImgSize d-flex justify-content-center" id="imgtext">
<figcaption style="color: #999999; font-size: 12px;" class="figure-caption">HERE IS THE TITLE</figcaption>
</div>
</div>
<!--END MAIN CONTENT-->
</div>
<!--col-md-9-->
</div>
But I want a function in which you click on the expanded-image and the next img would be shown and ideally in a loop. so that you could switch from the last image in the list to the first image in the list for n images.
Is there any elegant way of implement my desired function into this piece of code?
Like a function with n number of images and with the counter [i] and if [i] is equal to the number of images the first image will be shown?
But i don’t know how to count the elements/links or give them certain values so that you have a way to count and address the certain images.
I’ll try it by myself but I’m glad for every helping hand!!
2
Answers
Check my solution below
Could you use the data-* attribute to set numbers on each of the elements? Then in your function you could write the logic to cycle through the images, or go from last to first, or backwards from first to last.