How can I make the Bootstrap js carousel automatically cycle as soon as the page loads?

前端 未结 15 1511
花落未央
花落未央 2020-12-04 21:53

Using bootstrap.js version 2.02

I\'m trying to get the Twitter Bootstrap Carousel to automatically cycle as soon as someone visits my site. Right now, the auto cycl

相关标签:
15条回答
  • 2020-12-04 22:54

    Try this.

    $(document).ready(function ()
    {
        $('.carousel').carousel({interval:5000,pause:false});
    });
    
    0 讨论(0)
  • 2020-12-04 22:56

    if none of those solutions work for you. try this:

    $(document).ready(function () {
      function playcarousel(){
        $('.carousel-control.right').trigger('click');
      } 
      window.setInterval(playcarousel, 4000); 
    });
    
    0 讨论(0)
  • 2020-12-04 22:57

    Note: As mentioned in the comments this solution will work correctly for Bootstrap 2. Refer to MattZ's answer for instructions on how to accomplish the same behavior on Bootstrap 3.

    I had the same issue as you and all I had to do to fix it was call the carousel('cycle') method after I had initialized the carousel:

    <script type="text/javascript">
    $(document).ready(function () {
        $('.carousel').carousel({
            interval: 3000
        });
    
        $('.carousel').carousel('cycle');
    });
    </script>  
    

    I realize this question is old but I was googling tonight trying to figure it out myself and saw noone had properly answered it. Top voted answer will not automatically start the carousel, it will only cycle once a button has been pressed which is not what the OP was looking for.

    0 讨论(0)
提交回复
热议问题