Flexible Bootstrap Carousel Enhancement With jQuery

File Size: 2.88 KB
Views Total: 8184
Last Update:
Publish Date:
Official Website: Go to website
License: MIT
   
Flexible Bootstrap Carousel Enhancement With jQuery

A simple jQuery extension for Bootstrap for creating more flexible Bootstrap carousel component that supports multiple items in one slide and multiple instances on one page.

How to use it:

1. Make sure you first have Twitter Bootstrap framework installed in the html page.

<link rel="stylesheet" href="/path/to/bootstrap.min.css">
<script src="/path/to/bootstrap.min.js"></script>

2. Create a carousel and insert multiple items into a slide using Bootstrap's grid system.

<div id="example" class="carousel slide" data-ride="carousel">
  <div class="carousel-inner">
    <ul class="row item active">
      <li class="col-xs-4">
        <img src="1.jpg" srcset="1.jpg,[email protected]" class="img-responsive">
      </li>
      <li class="col-xs-4">
        <img src="2.jpg" srcset="2.jpg,[email protected]" class="img-responsive">
      </li>
      <li class="col-xs-4">
        <img src="3.jpg" srcset="3.jpg,[email protected]" class="img-responsive">
      </li>
    </ul>
    <ul class="row item">
      <li class="col-xs-4">
        <img src="4.jpg" srcset="4.jpg,[email protected]" class="img-responsive">
      </li>
      <li class="col-xs-4">
        <img src="5.jpg" srcset="5.jpg,[email protected]" class="img-responsive">
      </li>
      <li class="col-xs-4">
        <img src="6.jpg" srcset="6.jpg,[email protected]" class="img-responsive">
      </li>
    </ul>
  </div>

  <a class="carousel-control left" href="#example" data-slide="prev">Previous</a>
  <a class="carousel-control right" href="#example" data-slide="next">Next</a>
  
</div>

3. Apply your custom CSS styles to the carousel.

.carousel[data-shift="1"] .carousel-inner > .item {
  -webkit-transition: all .5s ease-in-out;
  transition: all .5s ease-in-out;
}

.carousel .carousel-inner > .item.row { margin: 0; }

.carousel .carousel-control {
  background: none;
  color: transparent;
  overflow: hidden;
  text-shadow: none;
  -webkit-transition: all .3s ease-in-out;
  transition: all .3s ease-in-out;
  width: 30px;
}

.carousel .carousel-control:before {
  color: #444;
  font-family: 'Glyphicons Halflings';
  position: absolute;
  top: 50%;
  -webkit-transform: translateY(-50%);
  transform: translateY(-50%);
  width: 30px;
}

.carousel .carousel-control:after {
  border-radius: 50%;
  box-shadow: 0 0 0 0 rgba(0, 0, 0, 0.7);
  content: '';
  height: 60%;
  position: absolute;
  top: 50%;
  -webkit-transform: translateY(-50%);
  transform: translateY(-50%);
  -webkit-transition: all .3s ease-in-out;
  transition: all .3s ease-in-out;
  width: 100%;
}

.carousel .carousel-control:hover:after { box-shadow: 0 0 30px 0 rgba(0, 0, 0, 0.7); }

.carousel .carousel-control.left { left: -30px; }

.carousel .carousel-control.left:before { content: '\e079'; }

.carousel .carousel-control.left:after { right: -100%; }

.carousel .carousel-control.right { right: -30px; }

.carousel .carousel-control.right:before { content: '\e080'; }

.carousel .carousel-control.right:after { left: -100%; }

4. Include the necessary jQuery library at the bottom of the html page.

<script src="//code.jquery.com/jquery-2.2.0.min.js"></script> 

5. The JavaScript (jQuery script) to reconstruct the carousel markup into more slides.

var carousels = $('.carousel');
carousels.each(function() {
  var $obj = $(this);
  var $inner = $obj.find('.carousel-inner');
  
  var id = 'uuid' + new Date().getTime();
  $obj.addClass(id);

  if ($obj.data('shift') === 1) {
    var items = $obj.find('.item > [class*="col-"]'),
        visibleCnt = $obj.find('.item:first [class*="col-"]').length,
        wrapper = "";
    
    // build styles
    var rule_base = '.carousel.' + id + ' .carousel-inner > .item',
        styles = $('<style></style>'),
        rules = [];
        rules[0] = rule_base + '.next {left: ' + (100 / visibleCnt) + '%; transform: none;}';
        rules[1] = rule_base + '.active {left: 0;}';
        rules[2] = rule_base + '.active.left {left: -' + (100 / visibleCnt) + '%; transform: none;}';
        rules[3] = rule_base + '.next.left {left: 0;}';
        rules[4] = rule_base + '.active.right {left: ' + (100 / visibleCnt) + '%; transform: none;}';
        rules[5] = rule_base + '.prev.right {left: 0;}';
        rules[6] = rule_base + '.prev {left: -' + (100 / visibleCnt) + '%; transform: none;}';
    for (var i = 0; i < rules.length; i++) {
      styles.append(rules[i]);
    }
    $obj.prepend(styles);

    // rebuild items
    for (var i = 0; i < $(items).length; i++) {
      var $item = $(items[i]);
      var parent = $item.parent();
      if (parent.hasClass('item')) {
        if (!wrapper.length) {
          wrapper = parent.clone().removeClass('active').html('');
        }
        $item.unwrap();
      }
      
      var itemGroup = [$item];
      for (var x = 1; x < visibleCnt; x++) {
        var a = i + x;
        var next = $(items[a]);
        if (!next.length) {
          next = $(items[(a - $(items).length)]);
        }
        itemGroup[x] = next.clone();
      }
      var newSet = wrapper.clone().html(itemGroup);
      if (i == 0) {
        newSet.addClass('active');
      }
      newSet.appendTo($inner);
    }
  }
});

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