I am looking to create a slideshow that features images with text overlay, and I want to implement next and previous buttons for image navigation. However, I am having trouble getting the opacity transition to work when switching between images by toggling classes. Everything else in the slideshow is functioning correctly, but the transition effect remains elusive. Any help or suggestions would be greatly appreciated! ^^
Below is my current code:
<section class="featured container section">
<div class="featured__slides">
<div class="featured__active featured__item">
<img class="featured__img" src="/featured/f1.jpg">
</div>
<div class="featured__unactive featured__item">
<img class="featured__img" src="/featured/f2.jpg">
</div>
</div>
<div class="featured__arrows">
<button class="featured__arrow featured__prev">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="currentColor">
<path fill-rule="evenodd" d="M7.72 12.53a.75.75 0 010-1.06l7.5-7.5a.75.75 0 111.06 1.06L9.31 12l6.97 6.97a.75.75 0 11-1.06 1.06l-7.5-7.5z" clip-rule="evenodd" />
</svg>
</button>
<button class="featured__arrow featured__next">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="currentColor">
<path fill-rule="evenodd" d="M16.28 11.47a.75.75 0 010 1.06l-7.5 7.5a.75.75 0 01-1.06-1.06L14.69 12 7.72 5.03a.75.75 0 011.06-1.06l7.5 7.5z" clip-rule="evenodd" />
</svg>
</button>
</div>
</section>
<style>
.featured {
flex-direction: column;
display: flex;
justify-content: center;
align-items: center;
overflow: hidden;
position: relative;
}
.featured__item {
display: none;
-webkit-transition: all .3s linear 0s;
transition: all .3s linear 0s;
opacity: 0;
}
.featured__active {
display: block;
opacity: 1;
}
.featured__arrows {
display: flex;
justify-content: space-between;
position: absolute;
left: 0;
right: 0;
margin-left: 0.5rem;
margin-right: 0.5rem;
}
.featured__arrow {
height: var(--size-lg);
width: var(--size-lg);
color: var(--clr-cyan400);
}
</style>
<script>
const nextArrow = document.getElementsByClassName("featured__next");
const prevArrow = document.getElementsByClassName("featured__prev");
var idx = 0;
var slides = document.getElementsByClassName("featured__slides");
var slideshowElements = $(slides).children();
$(nextArrow).click(function () {
slideshowElements[idx].classList.toggle("featured__active");
slideshowElements[idx].classList.toggle("featured__unactive");
if (slideshowElements.length - 1 == idx)
{
idx = 0;
}
else
{
idx++;
}
slideshowElements[idx].classList.toggle("featured__active");
slideshowElements[idx].classList.toggle("featured__unactive");
});
$(prevArrow).click(function () {
slideshowElements[idx].classList.toggle("featured__active");
slideshowElements[idx].classList.toggle("featured__unactive");
if (idx == 0)
{
idx = slideshowElements.length - 1;
}
else
{
idx--;
}
slideshowElements[idx].classList.toggle("featured__active");
slideshowElements[idx].classList.toggle("featured__unactive");
});
</script>
I have also attempted applying the transition effect to the "featured__img" class without success.