Create a unique Bootstrap 5 carousel featuring a single progress bar

Hey guys,

I'm having some trouble with my Bootstrap 5 carousel. I want to add a progress bar with left and right arrows, and also the number of slides displayed, just like in this image: https://i.sstatic.net/pqOMy.jpg

I did some research and found a couple of solutions, but they were all based on Bootstrap 3 and didn't work for Bootstrap 5. Finally, I stumbled upon a similar thread that had a JavaScript code, but it required manual clicking and didn't work automatically. So, I'm looking for a similar solution where the numbers have an active class that switches from 1-2 to 2-3, and so on, while also updating the progress bar according to the slides. Here's the thread for reference: Link

Any help would be greatly appreciated.

Here's what I have currently:

const myCarousel = document.getElementById("carouselExampleIndicators2");
const carouselIndicators = myCarousel.querySelectorAll(
  ".carousel-indicators button span"
);
let intervalID;

const carousel = new bootstrap.Carousel(myCarousel);

window.addEventListener("load", function () {
  fillCarouselIndicator(1);
});

myCarousel.addEventListener("slide.bs.carousel", function (e) {
  let index = e.to;
  fillCarouselIndicator(++index);
});

function fillCarouselIndicator(index) {
  let i = 0;
  for (const carouselIndicator of carouselIndicators) {
    carouselIndicator.style.width = 0;
  }
  clearInterval(intervalID);
  carousel.pause();

  intervalID = setInterval(function () {
    i++;

    myCarousel.querySelector(".carousel-indicators .active span").style.width =
      i + "%";

    if (i >= 100) {
      // i = 0; -> just in case
      carousel.next();
    }
  }, 50);
}
#carouselExampleIndicators2 .carousel-item p{ 
  font-family:'Giove',sans-serif; 
  text-transform: uppercase;
  margin:  16px 200px;
  font-size: 22px;
  color: #e9c8b9;
}

#carouselExampleIndicators2 .carousel-item p::before {
  content: url(../images/vec.png);
  position: absolute;
  top: 0%;
  left: 21.5%;
  opacity: 14%;
}

#carouselExampleIndicators2 .carousel-indicators [data-bs-target] {
  position: relative;
  width: 60px;
  height: 6px;
  border: none;
  border-radius: 24px;
}

#carouselExampleIndicators2 .carousel-indicators [data-bs-target] span {
  content: "";
  position: absolute;
  top: 0%;
  left: 0;
  width: 0;
  height: 100%;
  background: #bb440d;
  border-radius: inherit;
}
<script src="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="36545959424542445746760318061804">[email protected]</a>/dist/js/bootstrap.bundle.min.js"></script>
<link href="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="d6b4b9b9a2a5a2a4b7a696e3f8e6f8e4">[email protected]</a>/dist/css/bootstrap.min.css" rel="stylesheet"/>
<div class="container-fluid text-center section0 px-0 bg-dark">
  <div id="carouselExampleIndicators2" class="carousel slide" data-bs-ride="carousel" data-bs-pause="true">
    <div class="carousel-indicators">
      <button type="button" data-bs-target="#carouselExampleIndicators2" data-bs-slide-to="0" class="active" aria-current="true" aria-label="Slide 1">
        <span></span>
      </button>
      <button type="button" data-bs-target="#carouselExampleIndicators2" data-bs-slide-to="1" aria-label="Slide 2">
        <span></span>
      </button>
      <button type="button" data-bs-target="#carouselExampleIndicators2" data-bs-slide-to="2" aria-label="Slide 3">
        <span></span>
      </button>
    </div>
    <div class="carousel-inner pb-5">
      <div class="carousel-item active">
        <p>The most exclusive listings in Europe and the Middle East, <br>
          carefully handpicked to deliver state-of-the-art <br>
          customer experience.</p>
          <!-- <div class="number">1</div> -->
      </div>
      <div class="carousel-item">
        <p>The most exclusive listings in Europe and the Middle East, <br>
          carefully handpicked to deliver state-of-the-art <br>
          customer experience.</p>
      </div>
      <div class="carousel-item">
        <p>The most exclusive listings in Europe and the Middle East, <br>
          carefully handpicked to deliver state-of-the-art <br>
          customer experience.</p>
      </div>
    </div>
    <button class="carousel-control-prev" type="button" data-bs-target="#carouselExampleIndicators2" data-bs-slide="prev">
      <span class="carousel-control-prev-icon" aria-hidden="true"></span>
      <span class="visually-hidden">Previous</span>
    </button>
    <button class="carousel-control-next" type="button" data-bs-target="#carouselExampleIndicators2" data-bs-slide="next">
      <span class="carousel-control-next-icon" aria-hidden="true"></span>
      <span class="visually-hidden">Next</span>
    </button>
  </div>
 </div>

Answer №1

It seems like the solution you're searching for, although not necessarily the optimal structure, is fully responsive and functional.

const myCarousel = document.getElementById("carouselExampleIndicators2");
const carouselIndicators = myCarousel.querySelectorAll(
  ".carousel-indicators button span"
);
let intervalID;

const carousel = new bootstrap.Carousel(myCarousel);

window.addEventListener("load", function () {
  fillCarouselIndicator(1);
});

myCarousel.addEventListener("slide.bs.carousel", function (e) {
  let index = e.to;
  fillCarouselIndicator(++index);
});

function fillCarouselIndicator(index) {
  let i = 0;
  for (const carouselIndicator of carouselIndicators) {
    carouselIndicator.style.width = 0;
  }
  clearInterval(intervalID);
  carousel.pause();

  intervalID = setInterval(function () {
    i++;

    myCarousel.querySelector(".carousel-indicators .active span").style.width =
      i + "%";

    if (i >= 100) {
      // i = 0; -> just in case
      carousel.next();
    }
  }, 50);
}
#carouselExampleIndicators2 .carousel-item p{ 
  font-family:'Giove',sans-serif; 
  text-transform: uppercase;
  margin:  16px 200px;
  font-size: 22px;
  color: #e9c8b9;
}

#carouselExampleIndicators2 .carousel-item p::before {
  content: url(../images/vec.png);
  position: absolute;
  top: 0%;
  left: 21.5%;
  opacity: 14%;
}


#carouselExampleIndicators2 .carousel-indicators [data-bs-target] {
  position: absolute;
  width: 100px;
  height: 1.9px;
  border: 0.2px solid #e9c8b9;
  border-radius: 24px;
  background: transparent;
  cursor: default;
}

#carouselExampleIndicators2 .carousel-indicators [data-bs-target] span {
  content: "";
  position: absolute;
  top: 0%;
  left: 0%;
  width: 0;
  height: 100%;
  background: #e9c8b9;
  border-radius: inherit;
  cursor: default;
}
<script src="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="294b46465d5a5d5b4859691c0719071b">[email protected]</a>/dist/js/bootstrap.bundle.min.js"></script>
<link href="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="e7858888939493958697a7d2c9d7c9d5">[email protected]</a>/dist/css/bootstrap.min.css" rel="stylesheet"/>
<div class="container-fluid text-center section0 px-0 bg-dark">
  <div id="carouselExampleIndicators2" class="carousel slide" data-bs-ride="carousel" data-bs-pause="true">
    <div class="carousel-indicators">
      <button type="button" data-bs-target="#carouselExampleIndicators2" data-bs-slide-to="0" class="active" aria-current="true" aria-label="Slide 1">
        <span></span>
      </button>
      <button type="button" data-bs-target="#carouselExampleIndicators2" data-bs-slide-to="1" aria-label="Slide 2">
        <span></span>
      </button>
      <button type="button" data-bs-target="#carouselExampleIndicators2" data-bs-slide-to="2" aria-label="Slide 3">
        <span></span>
      </button>
    </div>
    <div class="carousel-inner pb-5">
      <div class="carousel-item active">
        <p>The most exclusive listings in Europe and the Middle East, <br>
          carefully handpicked to deliver state-of-the-art <br>
          customer experience.</p>
          <!-- <div class="number">1</div> -->
      </div>
      <div class="carousel-item">
        <p>The most exclusive listings in Europe and the Middle East, <br>
          carefully handpicked to deliver state-of-the-art <br>
          customer experience.</p>
      </div>
      <div class="carousel-item">
        <p>The most exclusive listings in Europe and the Middle East, <br>
          carefully handpicked to deliver state-of-the-art <br>
          customer experience.</p>
      </div>
    </div>
   
  </div>
 </div>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Sort slider items using Show/Hide feature in bxSlider

I am using a bxslider on my website with specific settings: $('#carousel').bxSlider({ slideWidth: 146, minSlides: 2, maxSlides: 6, speed:500, adaptiveHeight: true, moveSlides:3, infiniteLoop : false, hideContr ...

DreamFactory's REST API POST request for rest/user/session consistently encounters errors in Internet Explorer 9

In Firefox, Chrome, and Safari, the initial POST rest/user/session request works perfectly fine. However, in Internet Explorer 9, it consistently returns an error. When the dataType is specified as "json," IE9 encounters a 'no transport' error w ...

Guide on creating dynamic datasets and multiple dynamic yAxes

To enhance my chart, I am interested in adding multiple Y Axes based on the examples provided below: script 1 script 2 Although I attempted to modify the code as shown below, it seems to not be functioning properly. function rdnum() { return Math.flo ...

Nodemailer fails to send out emails despite the absence of any error messages

I'm currently working on setting up a confirmation email feature for user sign-ups on my website. I've tackled similar tasks in the past, but this time I've hit a roadblock - the emails are not being sent, and there are no error messages to ...

Switching icon with jQuery upon clicking

I'm just starting to learn jQuery and I'm working on changing the font icon class when clicked to display a drop down. Right now, it changes the icon and drops down the content as expected. However, I'm facing an issue where I want the icon ...

Create a responsive layout with Bootstrap that features five columns of equal width starting from the "md" breakpoint

I've implemented this code multiple times in Bootstrap 4 where columns have full width on mobile and equal width from "md" breakpoint: <div class="container"> <div class="row"> <div class="col-12 col-md&qu ...

Using setInterval on a batch of freshly generated div elements

I am interested in creating a small website where I can display multiple clocks for various time zones. However, I have encountered an issue with the setInterval function. Below is the code snippet: function addClock () { $("#container").append('& ...

Unable to retrieve variable using the require statement

When attempting to access the variable 'app' from the required index.js in my test, I encounter difficulty resolving the 'app' variable. server.js 'use strict'; var supertestKoa = require('supertest-koa-agent'); ...

Special function to handle ajax requests malfunctioning

I am seeking to develop a unique function for AJAX post requests. Below is the existing source code: function handleAjax(url, data) { var xhr = new XMLHttpRequest(); xhr.onreadystatechange = function() { if (xhr.readyState == 4 && xhr.st ...

Utilize the jQuery function as a callback argument

There is a jQuery plugin that I am currently working on: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head><title></title> <script type="text/javascript" sr ...

Utilizing jQuery to compute dynamic fields depending on selection in a dropdown menu

Creating a Ruby app for tracking bets, I have designed a small form that captures various details including date and match. However, the most crucial components of this form are the "stake" and "odd" text fields. To enhance user experience, I have incorpor ...

Error encountered when attempting to add headers to a request, resulting in an HTTP status code

Encountering difficulties when making get requests to a server with headers set using Axios/http request. Adding any header (excluding the content-type header) triggers the same error: "Response for preflight has invalid HTTP status code 405." This issue p ...

Tips for displaying only one image when clicked and hiding other divs:

Currently tackling a project that involves JavaScript and I've hit a roadblock. Before you dive into the text, take a look at the image. Check out the picture here. I have successfully created a slider, but now I want to implement a feature where cli ...

When the screen is at mobile width, elements with the class "Responsive" are hidden using the display:none; property. These elements can be

Hey there! So, I've got this cool interactive banner on my website. It features 2 slider sections with some awesome products on the right side. The layout is responsive, meaning that when you switch to a mobile screen size (around 515px or less), the ...

When resizing the window, the width change in JQuery always resets to 0

I've been trying to adjust the width of a div element when the window is resized, but for some reason, the width always ends up being 0 after the resize event. function resizeUploadField() { var uploadInputField = $('.input-append&apo ...

The multi-item carousel slider in Bootstrap 4.0.0 pen is experiencing issues when integrated with Bootstrap 4.5.3

After searching extensively, I finally stumbled upon a multi-item carousel compatible with bootstrap 4 that meets all my requirements: It is completely responsive (displaying only one item on smaller screens) It features animations <div class="co ...

arrow function implemented in a React hook for handling onClick event

From my understanding, placing an arrow function in the JSX creates a new reference of a new function each time it is triggered. For example: <p onClick={() => handleClick() /> In older versions of React with classes, we could do this: <p onCl ...

What is the best way to retrieve certain fields from a Firestore database using Next.js?

Is there a way to access a specific field in a document using the user's uid as its name? I am utilizing useAuthState from react-firebase-hooks to retrieve the user's credentials (uid). https://i.sstatic.net/t1xGL.png This code snippet allows me ...

Issues with fetching data from a Drupal module using an Ajax call

I have created a custom module in Drupal where the .js file is supposed to make an ajax call to a .module file. However, I am facing issues as the ajax call is not functioning properly. Can someone please assist me with this? Below is my .js file: // Jqu ...

How can I implement SSO and Auth for multiple sub-domains using PHP?

Is it possible to implement SSO using PHP between two different sub-domains (e.g. parappawithfries.com and *.parappawithfries.com)? My current configuration is causing issues as I use Cloudflare to direct my subs to a different 000webhost website. While I ...