Open In App

How to fix sliding Carousel in Bootstrap 5 ?

Last Updated : 30 Jan, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

In Bootstrap 5, a Sliding Carousel is a dynamic component that displays a series of images or content sequentially. It enables a visually appealing, automatic, or user-triggered transition between slides. Users can navigate through the slides using controls, creating an interactive and engaging content display.

Using Bootstrap and jQuery

For creating a fixed sliding carousel in Bootstrap 5 using jQuery, using the carousel component. Verify the accuracy of the Bootstrap and jQuery CDN links. Customize carousel items, add images, and style controls. Use jQuery for carousel activation, manual sliding, and hover-based pause/cycle functionality. jQuery is used to activate the carousel, enable manual sliding on item click, and pause/resume on hover. The navigation controls are customized with black backgrounds and rounded circles.

Example: Illustration of fixing sliding Carousel in Bootstrap 5 using jQuery.

HTML




<!DOCTYPE html>
<html lang="en">
  
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content=
          "width=device-width, initial-scale=1">
    <link href=
          rel="stylesheet">
    <title>Fixed Sliding Carousel</title>
</head>
  
<body>
  
    <div class="container mt-5">
        <div id="myCarousel" 
             class="carousel slide" 
             data-bs-ride="carousel">
            <div class="carousel-inner">
                <div class="carousel-item active  h-90">
                    <img src=
                         class="d-block w-100" 
                         alt="slide 1">
                </div>
                <div class="carousel-item  h-90">
                    <img src=
                        class="d-block w-100" 
                        alt="slide 2">
                </div>
                <div class="carousel-item  h-90">
                    <img src=
                        class="d-block w-100" 
                        alt="slide 3">
                </div>
            </div>
            <button class="carousel-control-prev" 
                    type="button" 
                    data-bs-target="#myCarousel" 
                    data-bs-slide="prev">
                <span class="carousel-control-prev-icon 
                             bg-black rounded-circle" 
                      aria-hidden="true">
                </span>
                <span class="visually-hidden">
                    Previous
                </span>
            </button>
            <button class="carousel-control-next" 
                    type="button" 
                    data-bs-target="#myCarousel" 
                    data-bs-slide="next">
                <span class="carousel-control-next-icon 
                             bg-black rounded-circle" 
                      aria-hidden="true">
                </span>
                <span class="visually-hidden">
                    Next
                </span>
            </button>
        </div>
    </div>
  
    <script src=
    </script>
    <script src=
    </script>
  
    <script>
  
        // Activate Carousel
        $('#myCarousel').carousel();
  
        // Enable Carousel Indicators
        $('.carousel-item').click(function () {
            $('#myCarousel').carousel($(this)
                            .index());
        });
  
        // Pause the carousel when the mouse is over it
        $('#myCarousel').hover(function () {
            $(this).carousel('pause');
        }, function () {
            $(this).carousel('cycle');
        });
    </script>
  
</body>
  
</html>


Output:

CarouselJQUERY

Using Vanilla JavaScript

For create a fixed sliding carousel using vanilla JavaScript by initializing the carousel, handling indicators with event listeners, and implementing pause and resume on mouse hover. Ensure consistent image sizes for a cohesive display.

Example: Illustration of fixing sliding Carousel in Bootstrap 5 using vanilla JavaScript.

HTML




<!DOCTYPE html>
<html lang="en">
  
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content=
          "width=device-width, initial-scale=1">
    <link href=
          rel="stylesheet">
    <title>Fixed Sliding Carousel</title>
</head>
  
<body>
  
    <div class="container mt-5">
        <div id="myCarousel" 
             class="carousel slide" 
             data-bs-ride="carousel">
            <div class="carousel-inner">
                <div class="carousel-item 
                            active h-90">
                    <img src=
                         class="mx-auto d-block w-80" 
                         alt="HTML">
                </div>
                <div class="carousel-item h-90">
                    <img src=
                         class="mx-auto d-block w-80" 
                         alt="CSS">
                </div>
  
            </div>
            <button class="carousel-control-prev" 
                    type="button" 
                    data-bs-target="#myCarousel" 
                    data-bs-slide="prev">
                <span class="carousel-control-prev-icon 
                             bg-black rounded-circle" 
                      aria-hidden="true">
                </span>
                <span class="visually-hidden">Previous</span>
            </button>
            <button class="carousel-control-next" 
                    type="button" 
                    data-bs-target="#myCarousel" 
                    data-bs-slide="next">
                <span class="carousel-control-next-icon 
                             bg-black rounded-circle" 
                      aria-hidden="true">
                </span>
                <span class="visually-hidden">
                    Next
                </span>
            </button>
        </div>
    </div>
  
    <script src=
    </script>
  
    <script>
        
        // Get carousel element
        var myCarousel = 
            document.getElementById('myCarousel');
  
        // Activate Carousel
        new bootstrap.Carousel(myCarousel);
  
        // Enable Carousel Indicators
        var carouselItems = 
            document.querySelectorAll('.carousel-item');
              carouselItems.forEach(function (item, index) {
                item.addEventListener('click', function () {
                    new bootstrap.Carousel(myCarousel).to(index);
            });
        });
  
        // Pause the carousel when the mouse is over it
        myCarousel.addEventListener('mouseenter', function () {
            new bootstrap.Carousel(myCarousel).pause();
        });
  
        myCarousel.addEventListener('mouseleave', function () {
            new bootstrap.Carousel(myCarousel).cycle();
        });
    </script>
  
</body>
  
</html>


Output:

CarouselVanilla



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads