Hey everyone! I’m working on my college project and need help with creating a slideshow component. I want to display content that includes both text and pictures, and when someone clicks a next button, it should switch to different content.
I tried making this work but I’m stuck. Here’s what I have so far:
let activeSlideIndex = 0;
const slides = document.querySelectorAll(".slideshow-wrapper .slide");
function showNextSlide() {
slides[activeSlideIndex].style.display = "none";
activeSlideIndex = (activeSlideIndex + 1) % slides.length;
slides[activeSlideIndex].style.display = "block";
}
function initializeSlideshow() {
slides.forEach((slide, index) => {
slide.style.display = index === 0 ? "block" : "none";
});
}
.slide {
display: flex;
align-items: center;
padding: 15px;
background: #f8f9fa;
border-radius: 8px;
margin: 10px 0;
}
.slide-content {
flex: 1;
padding-right: 20px;
}
.slide-image {
flex: 1;
}
.slide-image img {
width: 100%;
height: auto;
border-radius: 5px;
}
.nav-button {
background: #007bff;
color: white;
border: none;
padding: 10px 20px;
border-radius: 5px;
cursor: pointer;
}
<div class="slideshow-wrapper">
<div class="slide">
<div class="slide-content">
<h3>Summer Beach Vacation</h3>
<p>Discover amazing beaches and sunny weather perfect for relaxation.</p>
</div>
<div class="slide-image">
<img src="beach.jpg" alt="Beach scene" />
</div>
</div>
<div class="slide">
<div class="slide-content">
<h3>Mountain Adventure</h3>
<p>Experience breathtaking views and exciting hiking trails.</p>
</div>
<div class="slide-image">
<img src="mountain.jpg" alt="Mountain landscape" />
</div>
</div>
</div>
<button class="nav-button" onclick="showNextSlide()">Next →</button>
Can someone help me figure out what I’m doing wrong? I want it to cycle through the slides smoothly. Thanks!