DEV Community

Cover image for Building a Simple JQuery Carousel
Samuel Peters
Samuel Peters

Posted on

Building a Simple JQuery Carousel

Carousels also known as sliders or slideshows, are a popular way to display multiple images or content in a compact and visually appealing manner on a blog post. In this article, I'll guide you through the process of creating a simple carousel for your website, enhancing the user experience and engagement with your content.

Prerequisites

Before we begin, ensure you have the following resources ready:

  • A basic understanding of HTML, CSS, and JavaScript.
  • A set of images you want to feature in your carousel.
  • A text editor for code editing.

Step-by-Step Instructions

1. HTML Structure:

Start by setting up the HTML structure for your carousel. Create a new HTML file and add the following code:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, 
initial-scale=1.0">
    <title>Blog Post Carousel</title>
    <link rel="stylesheet" href="style.css">
</head>
<body>
    <div class="carousel-container">
        <div class="carousel">
            <div class="carousel-slide">
                <img src="image1.jpg" alt="Image 1">
            </div>
            <div class="carousel-slide">
                <img src="image2.jpg" alt="Image 2">
            </div>
            <div class="carousel-slide">
                <img src="image3.jpg" alt="Image 3">
            </div>
        </div>
    </div>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"> </script> <!-- jQuery Library -->
    <script src="script.js"></script>
</body>
</html>
Enter fullscreen mode Exit fullscreen mode

Include jQuery CDN (Content Delivery Network)
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>

In code Above, we have the basic HTML structure with a container for the carousel and three slides, each containing an image.

2. CSS Styles (style.css):

Next, add some CSS styles to make your carousel look visually appealing. Create a new CSS file (style.css) and add the following code:

/* Add basic styling for the carousel */
.carousel-container {
    width: 80%;
    margin: 0 auto;
    overflow: hidden;
}

.carousel {
    display: flex;
    transition: transform 0.3s ease;
}

.carousel-slide {
    flex: 0 0 100%;
}

img {
    max-width: 100%;
    height: auto;
}
Enter fullscreen mode Exit fullscreen mode

These styles ensures that the images are responsive and visual appealing

3. JavaScript Functionality (script.js):

To enable navigation within the carousel, you'll need some JavaScript. Create a new JavaScript file (script.js) and add the following code:

const $carousel = $(".carousel");
    const $slides = $(".carousel-slide");
    let currentIndex = 0;

    function showSlide(index) {
        if (index < 0) {
            currentIndex = $slides.length - 1;
        } else if (index >= $slides.length) {
            currentIndex = 0;
        }

        $carousel.css("transform", `translateX(-${currentIndex * 100}%)`);
    }

    if ($nextButton.length && $prevButton.length) {
        $nextButton.click(function() {
            currentIndex++;
            showSlide(currentIndex);
        });

        $prevButton.click(function() {
            currentIndex--;
            showSlide(currentIndex);
        });
    }

 const autoAdvanceInterval = 3000; // Change slide every 3 seconds

    setInterval(function() {
        currentIndex++;
        showSlide(currentIndex);
    }, autoAdvanceInterval);

Enter fullscreen mode Exit fullscreen mode

Outcome

More

Conclusion
Carousels are an excellent way to showcase images or content in an engaging and organized manner. Feel free to customize the images, styles, and functionality to match your blog's design and requirements. By following these steps, you can enhance your blog's visual appeal and provide a better user experience for your readers.

If you found the information valuable, please consider giving it a thumbs up by hitting the "Like" button and sharing it with your network.

Top comments (1)

Collapse
 
paraflu profile image
paraflu

This isn't jQuery but vanilla JavaScript... 😅