11

I am trying to have different duration for each slide as my some slides have large content and some small please give me a solution with fiddle

I tried this but it only works on slide animation timing:

.carousel-inner > .item {
    position: relative;
    display: none;
    -webkit-transition: 0.6s ease-in-out left;
    -moz-transition: 0.6s ease-in-out left;
    -o-transition: 0.6s ease-in-out left;
    transition: 0.6s ease-in-out left;
}
paulalexandru
  • 9,218
  • 7
  • 66
  • 94
Umer bin Siddique
  • 460
  • 1
  • 4
  • 13

5 Answers5

30

Bootstrap 3.1 carousel don't allow diferent duration for each slide, but it offers one method and one event that we can use in order to ahieve this.

We will use the slid.bs.carousel event to detect when the carousel has completed its slide transition and the .carousel('pause') option to stop the carousel from cycling through items.

We will use this attribute data-interval="x" on each carousel item with different time duration, so our html will look like this for example:

<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 have to do is to:

  1. detect when a new item is displayed using the slid.bs.carousel event
  2. check his duration
  3. pause the carousel using .carousel('pause')
  4. set a timeout with the duration of the item and after the duration completed we should unpause 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 at the begining to add a starting interval and i've set it to 1000ms but i remove it each time i pause the carousel duration-1000. I've used the lines below to resolve the first item problem because that item was not caught 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 is going crazy, that's why i clear the timeout each time the user press on the left and right arrow.

Here is my live example http://jsfiddle.net/paulalexandru/52KBT/, hope this response was helpful for you.

paulalexandru
  • 9,218
  • 7
  • 66
  • 94
  • Hi I'd like some help please if possible.. I'm having a problem with the duration of the sliders, it seems that the script doesn't work with the `data-duration`.. Here's the link to my post http://stackoverflow.com/questions/32198883/bootstrap-set-different-interval-on-each-carousel-item – ltdev Aug 26 '15 at 09:31
  • 2
    You have here everything you need in order to make it work, you got here even a working example. Now it's up to you to adapt it to your code. As you see in this jsfiddle example it works very fine, you just need to take some time and understand what you need to do. – paulalexandru Aug 26 '15 at 09:38
  • Can you please check my fiddle https://jsfiddle.net/nfm0zv6h/ what's wrong?? I have run through my code many times, but I can't figure out why the carousel starts, for the first time only, the 1st slide pauses for 4 sec, when it supposed to be 8 .. when it starts from the beggining again it pauses for 8sec as expected – ltdev Sep 03 '15 at 10:07
  • You need to load the bootstrap.js library using external file. Currently is missing. – paulalexandru Sep 03 '15 at 11:25
  • Sorry forgot it.. You can check now – ltdev Sep 03 '15 at 12:56
  • 2
    You did not add it. It is missing. Anyways, it seems that you have very big issues understanding html/css/javascript, so you should not use twitter bootstrap until you can handle basic things. You have everything that you need in this page, even a simple working example, if you can't replicate this it means you need to take if from the start, with the basic html, css and javascript. – paulalexandru Sep 03 '15 at 18:29
  • Duration interval is not working when html5 videos are included in carousel..Can anyone help me to set the duration of videos in this carousel? – sunilkjt Feb 27 '17 at 07:50
  • thank you @paulalexandru, but your solution disable on hover pause feature, is there anyway to solve this problem? – Mohse Taheri Mar 13 '19 at 13:41
  • @MohseTaheri Hi, please add a jsfiddle example with your code I will look it up and see if I can fix it. Cheers – paulalexandru Mar 14 '19 at 07:15
  • @paulalexandru sorry i don't see your comment until now, you can check it on your jsfiddle: http://jsfiddle.net/paulalexandru/52KBT/ I'm using firefox quantom on ubuntu – Mohse Taheri May 13 '19 at 08:28
3

First of all, thank you @paulalexandru. Your code at first didn't work for me, however making some changes, I was able to achieve the expected result. The main issue was related to the element not finding the duration-interval, so I used javascript instead of jquery (I'm still a beginner on it). So the following code worked for me (I'm keeping the old code as comment)

var t;
//var start = $('#myCarousel').find('.active').attr('data-interval');
var start = document.getElementsByClassName("item active")[0].getAttribute("data-interval");


t = setTimeout(function () {
    $('#myCarousel').carousel('next')
}, start);

$('#myCarousel').on('slid.bs.carousel', function () {

    clearTimeout(t);
    //var duration = $('#myCarousel').find('.active').attr('data-interval');
    var duration = document.getElementsByClassName("item active")[0].getAttribute("data-interval");

    $('#myCarousel').carousel('pause');
    t = setTimeout("$('#myCarousel').carousel('next');", duration);
    console.log(duration);
})

$('.carousel-control.right').on('click', function () {
    clearTimeout(t);
});

$('.carousel-control.left').on('click', function () {
    clearTimeout(t);
});

The HTML

<div class="carousel-inner" role="listbox">
    <div class="item active" data-interval="3000">
        <img class="first-slide carousel-image-background">
            <div class="container">
                <div class="carousel-caption">
                    <h2 class="carousel-h2">My title</h2>
                    <p class="carousel-p">My description text.</p>
                </div>
            </div>
    </div>
</div>
2

For some reason, the pause method did seem to work for me. So, I used the following code and it worked for me. This works even if you have multiple carousels on the same page. This approach however requires the data-interval attribute mandatory against each carousel item.

var t;
var start = $('#myCarousel').find('.active').attr('data-interval');

t = setTimeout(function () {
    $('#myCarousel').carousel('next')
}, start);

$('#myCarousel').on('slid.bs.carousel', function () {

    clearTimeout(t);
    var duration = $('#myCarousel').find('.active').attr('data-interval');

    //$('#myCarousel').carousel('pause');
    t = setTimeout("$('#myCarousel').carousel('next');", duration);
})

$('.carousel-control.right').on('click', function () {
    clearTimeout(t);
});

$('.carousel-control.left').on('click', function () {
    clearTimeout(t);
});
goldsun
  • 21
  • 1
  • This is cleaner IMO. The pause on @paulalexandru 's otherwise excellent post added unnecessary complexity, especially when using modified crossfade (transition) times. Make sure you leave off the data-ride="carousel" of the carousel itself. Using the code above, javascript handles all the slide change work. – secretwep Mar 13 '19 at 18:10
-2
$('.carousel .item').hasClass('active', function() {
    var SlideInterval = $(this).attr('data-interval');
    $('.carousel').carousel({ interval: SlideInterval });
});
mx0
  • 6,445
  • 12
  • 49
  • 54
Wixe
  • 1
  • Welcome to Stack Overflow! Thank you for this code snippet, which may provide some immediate help. A proper explanation [would greatly improve](//meta.stackexchange.com/q/114762) its educational value by showing *why* this is a good solution to the problem, and would make it more useful to future readers with similar, but not identical, questions. Please [edit] your answer to add explanation, and give an indication of what limitations and assumptions apply. – Toby Speight Jun 02 '17 at 08:33
  • This doesn't solve the feature of having a different interval per item, but simply sets the same interval for all items. – Michael Jun 27 '18 at 23:52
-2

In case if you are looking for Angular solution, please below link

Add interval for each mdb-carousel item in Angular

Here is the extract from the link: In HTML, we need to pass the carousel component back to the Typescript using activeSlideChange method available for the slide event. Also need to add ids for each carousel item for reference

-- HTML

<div>
  <mdb-carousel  #carousal  [interval]="1000" [isControls]="false" class="carousel slide carousel-fade" [animation]="'fade'"
  (activeSlideChange)="activeSlideChange(carousal)" >
    <mdb-carousel-item id='home'>
      <!-- THIS NEEDS TO BE DISPLAYED FOR 5 SECS -->
      <img class="d-block w-100" src="https://mdbootstrap.com/img/Photos/Slides/img%20(68).jpg" alt="First slide">
    </mdb-carousel-item>

    <mdb-carousel-item id='time'>
      <!-- THIS NEEDS TO BE DISPLAYED FOR 15 SECS -->
      <img class="d-block w-100" src="https://mdbootstrap.com/img/Photos/Slides/img%20(6).jpg" alt="Second slide">
    </mdb-carousel-item>
  </mdb-carousel>
</div>

In typescript, we can define the activeSlideChange method like this

 activeSlideChange(cc: CarouselComponent){
   let slideItem = cc.slides[cc.activeSlide];
   let id = slideItem.el.nativeElement.id;
   console.log(id);
    if ( id === 'home') {
        cc.interval = 1000 * 5 ; // 5 secs
    } else if ( id === 'time') { 
      cc.interval = 1000 * 15; // 15 secs
    }else  {
      cc.interval = 5000;
    }
  }
Shahid
  • 481
  • 1
  • 8
  • 22
  • While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - [From Review](/review/low-quality-posts/29181359) – PeterJ Jun 12 '21 at 12:38
  • I thought of avoiding duplicating the solution. However it make sense if the link will be deleted. Thanks for your review – Shahid Jun 12 '21 at 15:09