Search code examples
javascripttwitter-bootstrapcarousel

Twitter Bootstrap Carousel autoplay on load


Using the twitter bootstrap framework, how is it possible to invoke the carousel to 'auto slide.' Meaning when the page loads, the carousel automatically scrolls.

I have tried a javascript onLoad click function for the <a> of the next link, but this didn't work.


Solution

  • you should do as the Twitter Bootstrap Documentation says about Carousel, so set the first Carousel slide item with class="active" and initialize the js for the carousel in this way:

    $(function(){
        $('.carousel').carousel({
          interval: 2000
        });
    });
    

    then if it's not enough (but this never happened to me) use the bruteforce triggering the click hover the carousel control buttons like this:

    $(function(){
    $('.carousel').carousel({
          interval: 2000
        });
    $('.carousel-control.right').trigger('click');
    });
    

    but this is just a not-needed trick, really, just follow the documentantion!