contains all your slides (here just text for simplicity).
–
Navigation buttons: We’ve got ‘Prev’ and ‘Next’ buttons to navigate through the slides.
Styling with CSS
Before jumping into JavaScript, let’s make things look pretty with a bit of CSS.
css
body {
font-family: Arial, sans-serif;
display: flex;
justify-content: center;
align-items: center;
min-height: 100vh;
background-color: #f4f4f9;
margin: 0;
}
.carousel {
position: relative;
overflow: hidden;
width: 300px;
}
.slides {
display: flex;
transition: transform 0.5s ease-in-out;
}
.slide {
min-width: 100%;
box-sizing: border-box;
background: #333;
color: #fff;
padding: 40px;
text-align: center;
font-size: 1.5rem;
}
button {
position: absolute;
top: 50%;
transform: translateY(-50%);
background-color: rgba(255, 255, 255, 0.7);
border: none;
cursor: pointer;
padding: 10px;
font-size: 1rem;
}
.prev {
left: 10px;
}
.next {
right: 10px;
}
Breakdown
– Slides as flex items: They line up horizontally, each taking full carousel width.
– Smooth transitions: The transition property makes the slide motion smooth.
– Button Styles: Positioned for easy navigation, slightly transparent for a sleek look.
Bringing It to Life with JavaScript
Now, here’s where the magic happens! We’ll use JavaScript to move the slides around.
javascript
const slides = document.querySelector('.slides');
const slide = document.querySelectorAll('.slide');
const prevButton = document.querySelector('.prev');
const nextButton = document.querySelector('.next');
let currentIndex = 0;
function showSlide(index) {
slides.style.transform = translateX(${-index * 100}%);
}
nextButton.addEventListener('click', () => {
currentIndex = (currentIndex + 1) % slide.length;
showSlide(currentIndex);
});
prevButton.addEventListener('click', () => {
currentIndex = (currentIndex === 0) ? slide.length - 1 : currentIndex - 1;
showSlide(currentIndex);
});
// Initialize the first slide
showSlide(currentIndex);
Breakdown
– Selecting elements: Grab the slides and buttons using querySelector.
– Indexing: currentIndex keeps track of which slide is visible.
– Slide transitions: Using transform: translateX(), we shift the slides.
– Event Listeners: Our buttons update currentIndex, updating the visible slide.
Testing It Out
Once you’ve got all this code in place, feel free to run it in your browser. Click on those ‘Prev’ and ‘Next’ buttons. Voilà! The slides move back and forth like magic.

Conclusion
And there you have it, a simple yet elegant carousel slider built with pure JavaScript! 🚀 Now, how about some practice to embed this knowledge?
Optional Practice Ideas
1. Add More Slides: Expand the slider by adding more slides and see how it handles them.
2. Auto-Play Feature: Enhance your slider by making it rotate slides automatically every few seconds.
3. Infinity Loop: Implement a feature to make the carousel rotate endlessly in one direction.
Remember, the best way to learn is by doing, so get your hands on a keyboard and start coding! If you have any questions or run into issues, feel free to drop a comment on the blog. Happy coding!
(Visited 14 times, 1 visits today)