Responsive Bootstrap Carousel With Multiple Items Per Slide

Responsive Bootstrap Carousel With Multiple Items Per Slide
File Size: 2.89 KB
Views Total:
Last Update:
Publish Date:
Official Website: Go to website
License: MIT
   

A lightweight extension to the Bootstrap 4 carousel component that allows for multiple items per slide and advances one slide at a time.

The plugin also supports responsive breakpoints that dynamically checks the screen width using CSS media queries and shows 3 items on desktop, 2 items on tablet, 1 item on mobile.

How to use it:

1. Download and insert the main script on the Bootstrap project.

<!-- Bootstrap Stylesheet -->
<link rel="stylesheet" href="/path/to/bootstrap.min.css" />
<!-- Bootstrap JS -->
<script src="/path/to/jquery.min.js"></script>
<script src="/path/to/bootstrap.min.js"></script>
<!-- Carousel Extension -->
<script src="carousel.js"></script>

2. The markup for the multi-slide carousel.

<div id="myCarousel" class="carousel slide w-100" data-ride="carousel">
  <div class="carousel-inner w-100" role="listbox">
    <div class="carousel-item active">
      <div class="col-lg-4 col-md-6">
        <img class="img-fluid" src="http://placehold.it/350x180?text=1">
      </div>
    </div>
    <div class="carousel-item">
      <div class="col-lg-4 col-md-6">
        <img class="img-fluid" src="http://placehold.it/350x180?text=2">
      </div>
    </div>
    <div class="carousel-item">
      <div class="col-lg-4 col-md-6">
        <img class="img-fluid" src="http://placehold.it/350x180?text=3">
      </div>
    </div>
    <div class="carousel-item">
      <div class="col-lg-4 col-md-6">
        <img class="img-fluid" src="http://placehold.it/350x180?text=4">
      </div>
    </div>
    <div class="carousel-item">
      <div class="col-lg-4 col-md-6">
        <img class="img-fluid" src="http://placehold.it/350x180?text=5">
      </div>
    </div>
    <div class="carousel-item">
      <div class="col-lg-4 col-md-6">
        <img class="img-fluid" src="http://placehold.it/350x180?text=6">
      </div>
    </div>
  </div>
  <a class="carousel-control-prev bg-dark w-auto" href="#myCarousel" role="button" data-slide="prev">
    <span class="carousel-control-prev-icon" aria-hidden="true"></span>
    <span class="sr-only">Previous</span>
  </a>
  <a class="carousel-control-next bg-dark w-auto" href="#myCarousel" role="button" data-slide="next">
    <span class="carousel-control-next-icon" aria-hidden="true"></span>
    <span class="sr-only">Next</span>
  </a>
</div>

3. Determine the number of items per slide in the CSS media queries as follows.

/* medium - display 2  */
@media (min-width: 768px) {

  .carousel-inner .carousel-item-right.active,
  .carousel-inner .carousel-item-next {
      transform: translateX(50%);
  }

  .carousel-inner .carousel-item-left.active,
  .carousel-inner .carousel-item-prev {
      transform: translateX(-50%);
  }
}

/* large - display 3 */
@media (min-width: 992px) {

  .carousel-inner .carousel-item-right.active,
  .carousel-inner .carousel-item-next {
      transform: translateX(33%);
  }

  .carousel-inner .carousel-item-left.active,
  .carousel-inner .carousel-item-prev {
      transform: translateX(-33%);
  }
}

@media (max-width: 768px) {
  .carousel-inner .carousel-item>div {
      display: none;
  }

  .carousel-inner .carousel-item>div:first-child {
      display: block;
  }
}

.carousel-inner .carousel-item.active,
.carousel-inner .carousel-item-next,
.carousel-inner .carousel-item-prev {
  display: flex;
}

.carousel-inner .carousel-item-right,
.carousel-inner .carousel-item-left {
  transform: translateX(0);
}

This awesome jQuery plugin is developed by MeneerOom. For more Advanced Usages, please check the demo page or visit the official website.