Home > Article > Web Front-end > Build a Simple Auto-Play Carousel with Clickable Controls Using Alpine.js
Here's a step-by-step example of creating a simple carousel using Alpine.js. Alpine.js is a lightweight JavaScript framework that provides reactivity and can be used to build interactive components without a lot of JavaScript.
In this example, we'll create a basic carousel that displays images one at a time, with "Previous" and "Next" buttons to navigate through them. Let's get started!
First, we’ll include Alpine.js in the head of our HTML file. You can do this by adding the following script tag:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Alpine.js Carousel</title> <script src="https://cdn.jsdelivr.net/npm/alpinejs@3.x.x/dist/cdn.min.js" defer></script> <style> .carousel { display: flex; justify-content: center; align-items: center; width: 100%; height: 300px; overflow: hidden; position: relative; } .carousel img { width: 100%; transition: opacity 0.5s ease-in-out; opacity: 0; } .carousel img.active { opacity: 1; } </style> </head> <body>
Inside the body, create a div for the carousel component and initialize it with x-data to define Alpine.js properties and methods.
<div x-data="carousel()" class="carousel"> <!-- Previous Button --> <button @click="prev" class="absolute left-0 bg-gray-700 text-white px-3 py-2 rounded">Previous</button> <!-- Carousel Images --> <template x-for="(image, index) in images" :key="index"> <img :src="image" :class="{'active': index === currentIndex}" alt="Carousel Image"> </template> <!-- Next Button --> <button @click="next" class="absolute right-0 bg-gray-700 text-white px-3 py-2 rounded">Next</button> </div>
Now we’ll define the carousel functionality in an Alpine component, setting the initial data and methods for navigating the images.
<script> function carousel() { return { currentIndex: 0, // Track the index of the current image images: [ 'https://via.placeholder.com/600x300?text=Slide+1', 'https://via.placeholder.com/600x300?text=Slide+2', 'https://via.placeholder.com/600x300?text=Slide+3', ], interval: null, startAutoPlay() { this.interval = setInterval(() => { this.next(); }, 3000); // Change every 3 seconds }, stopAutoPlay() { clearInterval(this.interval); }, // Method to go to the next image next() { this.currentIndex = (this.currentIndex + 1) % this.images.length; }, // Method to go to the previous image prev() { this.currentIndex = (this.currentIndex - 1 + this.images.length) % this.images.length; }, init() { this.startAutoPlay(); } }; } </script>
Carousel HTML Structure:
Alpine.js Data and Methods:
We added basic CSS styles for the carousel and buttons for positioning and visibility. The CSS transitions give the images a fade-in effect.
This example provides both auto-play functionality and clickable controls, making the carousel interactive. Let me know if you'd like further customization or additional features!
Connect with me:@ LinkedIn and checkout my Portfolio.
Please give my GitHub Projects a star ⭐️
Source Code
The above is the detailed content of Build a Simple Auto-Play Carousel with Clickable Controls Using Alpine.js. For more information, please follow other related articles on the PHP Chinese website!