Currently in the process of developing a basic image carousel for a practice website I'm working on. I've been following this tutorial on YouTube. At the 7:10 mark, the tutorial demonstrates the specific function that I need help with.
The tutorial introduces a jQuery function to activate the carousel.
$(document).ready(function(){
$(".nextLink").on("click", function(){
var currentActiveImage = $(".image-shown");
var nextActiveImage = currentActiveImage.next();
currentActiveImage.removeClass(".image-shown").addClass(".image-hidden");
nextActiveImage.addClass(".image-shown").removeClass("image-hidden");
});
});
Below is the HTML structure
<div class="carousel-outer">
<figure class="carousel-inner">
<img class="image-shown" src="images/team/alex_morrales.jpg" alt="">
<img class="image-hidden" src="images/team/david_kim.jpg" alt="">
<img class="image-hidden" src="images/team/jenny_tabers.jpg" alt="">
<img class="image-hidden" src="images/team/joey_barrett.jpg" alt="">
<img class="image-hidden" src="images/team/melinda_lee.jpg" alt="">
<img class="image-hidden" src="images/team/rachel_dotson.jpg" alt="">
</figure>
<div class="img-buttons-box">
<div class="img-buttons">
<a class="previousLink" href="#">Previous</a>
<a class="nextLink" href="#">Next</a>
</div>
</div>
</div>
And here's the accompanying CSS style
.image-shown {
display: inline-block;
}
.image-hidden {
display: none;
}
I initially believed that with the above function alone, the carousel would operate smoothly and cycle through the images seamlessly.
The concept being to always show the next photo while hiding the rest.
However, it seems this isn't the case as shown by the before and after screenshots below (the "next" button doesn't work upon subsequent clicks).
Before
https://i.sstatic.net/AdP0x.png
After
https://i.sstatic.net/3V37C.png
If anyone can shed light on why the existing function falls short in making the carousel functional, any insights would be greatly appreciated. Thank you.