The Bootstrap 3.1 carousel does not allow a different duration for each slide, but it offers one method and one event that we can use to confirm this.
We will use the slid.bs.carousel event to determine when the carousel has completed the slide transition and the .carousel('pause') parameter to stop the carousel from cycling through objects.
We will use this attribute data-interval="x" for each element of the carousel with a different time duration, so our html will look like this:
<div class="carousel-inner"> <div class="active item" data-interval="3000"> <img src="Url 1" /> </div> <div class="item" data-interval="6000"> <img src="Url 2" /> </div> <div class="item" data-interval="9000"> <img src="Url 3" /> </div> </div>
Now all we need to do is:
- detects when a new item is displayed using the
slid.bs.carousel event - check its duration
- pause the carousel with
.carousel('pause') - set a timeout with the duration of the element and after the duration is completed, we must abandon the carousel
The javascript code will look like this:
var t; var start = $('#myCarousel').find('.active').attr('data-interval'); t = setTimeout("$('#myCarousel').carousel({interval: 1000});", start-1000); $('#myCarousel').on('slid.bs.carousel', function () { clearTimeout(t); var duration = $(this).find('.active').attr('data-interval'); $('#myCarousel').carousel('pause'); t = setTimeout("$('#myCarousel').carousel();", duration-1000); }) $('.carousel-control.right').on('click', function(){ clearTimeout(t); }); $('.carousel-control.left').on('click', function(){ clearTimeout(t); });
As you can see, we are forced to add an initial interval at the beginning, and I set it to 1000 ms, but I delete it every time I pause the duration-1000 carousel. I used the lines below to solve the problem with the first element, because this element was not captured by the slid event .
var start = $('#myCarousel').find('.active').attr('data-interval'); t = setTimeout("$('#myCarousel').carousel({interval: 1000});", start-1000);
I also noticed that if the user presses the arrows, the timeout goes crazy, so I clear the timeout every time the user presses the left and right arrow.
Here is my live example http://jsfiddle.net/paulalexandru/52KBT/ , hope this answer was useful for you.