How to create a review carousel

To create a review carousel using JavaScript, you can utilize the concept of sliding elements and dynamically updating the content based on user interactions. Here's an example of how you can implement a review carousel:

HTML Code:
<!DOCTYPE html>
<html>
<head>
    <title>Review Carousel</title>
    <style>
        body {
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100vh;
            margin: 0;
            font-family: Arial, sans-serif;
        }

        .carousel {
            width: 500px;
            height: 300px;
            overflow: hidden;
            position: relative;
        }

        .carousel-content {
            display: flex;
            width: 100%;
            height: 100%;
            transition: transform 0.3s ease-in-out;
        }

        .carousel-item {
            width: 100%;
            height: 100%;
            display: flex;
            justify-content: center;
            align-items: center;
            font-size: 24px;
            background-color: lightgray;
        }

        .prev-button,
        .next-button {
            position: absolute;
            top: 50%;
            transform: translateY(-50%);
            font-size: 24px;
            background-color: rgba(0, 0, 0, 0.5);
            color: white;
            padding: 10px 20px;
            cursor: pointer;
            transition: background-color 0.3s ease-in-out;
        }

        .prev-button:hover,
        .next-button:hover {
            background-color: rgba(0, 0, 0, 0.8);
        }
    </style>
</head>
<body>
    <div class="carousel">
        <div class="carousel-content">
            <div class="carousel-item">Review 1</div>
            <div class="carousel-item">Review 2</div>
            <div class="carousel-item">Review 3</div>
        </div>
        <div class="prev-button" onclick="prevSlide()">&#10094;</div>
        <div class="next-button" onclick="nextSlide()">&#10095;</div>
    </div>

    <script src="review-carousel.js"></script>
</body>
</html>

JavaScript (review-carousel.js):

let currentSlide = 0;
const carouselItems = document.querySelectorAll('.carousel-item');

function showSlide() {
    carouselItems.forEach((item, index) => {
        if (index === currentSlide) {
            item.style.transform = 'translateX(0)';
        } else {
            item.style.transform = 'translateX(100%)';
        }
    });
}

function prevSlide() {
    currentSlide = (currentSlide - 1 + carouselItems.length) % carouselItems.length;
    showSlide();
}

function nextSlide() {
    currentSlide = (currentSlide + 1) % carouselItems.length;
    showSlide();
}

showSlide();

In this example, we have an HTML structure for the review carousel. The carousel container has a fixed width and height, with a hidden overflow to contain the carousel items. Each carousel item is represented by a `<div>` element with the class "carousel-item" and contains the review content.

The JavaScript code handles the functionality of the carousel. It defines a `currentSlide` variable to keep track of the currently displayed slide and selects all the carousel items using `document.querySelectorAll('.carousel-item')`.

The `showSlide` function is responsible for displaying the appropriate slide based on the `currentSlide` value. It uses a loop to iterate through all the carousel items and applies a transform.


Output:

img


About the Author



Silan Software is one of the India's leading provider of offline & online training for Java, Python, AI (Machine Learning, Deep Learning), Data Science, Software Development & many more emerging Technologies.

We provide Academic Training || Industrial Training || Corporate Training || Internship || Java || Python || AI using Python || Data Science etc





 PreviousNext