How can I make the Bootstrap js carousel automatic

2019-01-13 10:55发布

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 cycling works only after you click one of the cycle buttons at least once.

I want the carousel to begin cycling at the interval right away.

Does anyone know how to do this?

My site is hotairraccoon.com

You'll see how after you click the carousel once, it begins to cycle every 5 seconds or so, but I don't want the click to be required to reveal carousel content.

Thanks!

15条回答
smile是对你的礼貌
2楼-- · 2019-01-13 11:15

There is one more way to automatically cycle the bootstrap carousel.

use data-ride="carousel" attribute. It tells the bootstrap to begin animating the carousel immediately when the page loads.

查看更多
萌系小妹纸
3楼-- · 2019-01-13 11:17

Not sure if you got this sorted out, but I was having that problem as well. I resolved it by wrapping the carousel interval setting in a function like this:

!function ($) {
$(function() {
    $('.carousel').carousel({ interval: 3000 })
});
}(window.jQuery);
查看更多
Juvenile、少年°
4楼-- · 2019-01-13 11:21

A quick and dirty solution is to programmatically click the button on document ready:

$(function() {
  $(".right.carousel-control").click();
});

BTW: make sure you load jQuery before the other scripts referring to $, right now you have two Uncaught ReferenceError: $ is not defined because you call $ on line 101 and 182, but jquery is first loaded on line 243.

I would recommend using a tool like firebug or developer tool (chrome/safari) to catch these errors.

EDIT: I think you already have a working solution, but because you use jquery before it's loaded it doesn't work.

查看更多
劫难
5楼-- · 2019-01-13 11:25

Try to initialize your carousel like this:

$('.carousel').carousel().next();
查看更多
Lonely孤独者°
6楼-- · 2019-01-13 11:27

The jquery.js file must load before the bootstrap.js, whether they are placed in the head tags or at the end of your file.

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
<script src="bootstrap/js/bootstrap.js"type="text/javascript"></script>
查看更多
ら.Afraid
7楼-- · 2019-01-13 11:29

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

$(document).ready(function () {
  function playcarousel(){
    $('.carousel-control.right').trigger('click');
  } 
  window.setInterval(playcarousel, 4000); 
});
查看更多
登录 后发表回答