Myoji Myoji - 6 months ago 44
jQuery Question

Custom Carousel Intervals?

In Bootstrap 3, with jQuery is there a way to sort by the index of my carousel and add custom intervals of each slide so that I can have one slide 10000ms and another 500ms etc?

I know you can set the data-interval attribute but this sets the speed for all slides as you can't add a custom interval attribute to each item.

data-interval="3000"


My carousel is setup like this:

<div id="carousel" class="carousel slide">

<div class="carousel-inner">

<div class="item active">
<a href="#">
<img class="img-responsive" src="">
</a>
</div>

<div class="item">
<a href="#">
<img class="img-responsive" src="">
</a>
</div>

<div class="item">
<a href="#">
<img class="img-responsive" src="">
</a>
</div>

<div class="item">
<a href="#" >
<img class="img-responsive" src="">
</a>
</div>
</div>

Answer

You can create a custom attribute that denotes how long the slide should be visible for, pull that out for the active item on the slide.bs.carousel or slid.bs.carousel events (whichever you prefer/works best for you), then set it as a timeout to go to the next slide.

$('#carousel-example-generic').on('slide.bs.carousel', function() {
  var interval = $('div.item.active').attr('duration');
  setTimeout(function() {
    $('.carousel').carousel('next');
  }, interval);
});
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>


<div id="carousel-example-generic" class="carousel slide" data-ride="carousel">
  <!-- Indicators -->
  <ol class="carousel-indicators">
    <li data-target="#carousel-example-generic" data-slide-to="0" class="active"></li>
    <li data-target="#carousel-example-generic" data-slide-to="1"></li>
    <li data-target="#carousel-example-generic" data-slide-to="2"></li>
    <li data-target="#carousel-example-generic" data-slide-to="3"></li>
  </ol>

  <!-- Wrapper for slides -->
  <div class="carousel-inner" role="listbox">
    <div class="item active" duration="3000">
      <img src="http://placehold.it/400x200" alt="..." />
      <div class="carousel-caption">
        First
      </div>
    </div>
    <div class="item" duration="2000">
      <img src="http://placehold.it/400x200" alt="..." />
      <div class="carousel-caption">
        Second
      </div>
    </div>
    <div class="item" duration="1000">
      <img src="http://placehold.it/400x200" alt="..." />
      <div class="carousel-caption">
        Third
      </div>
    </div>
    <div class="item" duration="500">
      <img src="http://placehold.it/400x200" alt="..." />
      <div class="carousel-caption">
        Fourth
      </div>
    </div>
  </div>

  <!-- Controls -->
  <a class="left carousel-control" href="#carousel-example-generic" role="button" data-slide="prev">
    <span class="glyphicon glyphicon-chevron-left" aria-hidden="true"></span>
    <span class="sr-only">Previous</span>
  </a>
  <a class="right carousel-control" href="#carousel-example-generic" role="button" data-slide="next">
    <span class="glyphicon glyphicon-chevron-right" aria-hidden="true"></span>
    <span class="sr-only">Next</span>
  </a>
</div>

Comments