按照DOCO,使用以下將設置轉盤循環速度:如何更改引導傳送帶的間隔一旦被初始化
$('.carousel').carousel({
interval: 2000
})
但是,如果你已經初始化傳送帶,稱上述以不同的間隔不起作用。
我應該注意,通過JS初始化傳送帶不會在傳送帶上設置data-interval
。我也在這個話題上搜索了很多,但結果都是關於人們試圖以固定的速度建立的。一旦它已經被初始化,我想改變它的速度。
的代碼如下:
$(function() {
$('#homeCarousel').carousel({
interval:2000,
pause: "false"
});
$('#slowButton').click(function() {
$('#homeCarousel').carousel({interval: 10000});
});
$('#fastButton').click(function() {
$('#homeCarousel').carousel({interval: 1000});
});
});
#carouselButtons {
margin-left: 100px;
position: absolute;
bottom: 0px;
}
.item {
color: white;
background-color: black;
width:100%;
height: 350px;
}
<link href="http://netdna.bootstrapcdn.com/bootstrap/3.0.0/css/bootstrap.min.css" rel="stylesheet"/>
<link href="http://netdna.bootstrapcdn.com/font-awesome/3.2.1/css/font-awesome.min.css" rel="stylesheet"/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="http://netdna.bootstrapcdn.com/bootstrap/3.0.0/js/bootstrap.min.js"></script>
<!-- Carousel -->
<div id="homeCarousel" class="carousel slide">
<!-- Menu -->
<ol class="carousel-indicators">
<li data-target="#myCarousel" data-slide-to="0" class="active"></li>
<li data-target="#myCarousel" data-slide-to="1"></li>
<li data-target="#myCarousel" data-slide-to="2"></li>
</ol>
<!-- Items -->
<div class="carousel-inner">
<!-- Item 1 -->
<div class="item active">
<div class="container">
<div class="carousel-caption">
<h1>Bootstrap 3 Carousel Layout</h1>
<p>This is an example layout with carousel that uses the Bootstrap 3 styles.</p>
<p><a class="btn btn-lg btn-primary" href="http://getbootstrap.com">Learn More</a>
</p></div>
</div>
</div>
<!-- Item 2 -->
<div class="item">
<div class="container">
<div class="carousel-caption">
<h1>Changes to the Grid</h1>
<p>Bootstrap 3 still features a 12-column grid, but many of the CSS class names have completely changed.</p>
<p><a class="btn btn-large btn-primary" href="#">Learn more</a></p>
</div>
</div>
</div>
<!-- Item 3 -->
<div class="item">
<div class="container">
<div class="carousel-caption">
<h1>Percentage-based sizing</h1>
<p>With "mobile-first" there is now only one percentage-based grid.</p>
<p><a class="btn btn-large btn-primary" href="#">Browse gallery</a></p>
</div>
</div>
</div>
</div>
<!-- Controls -->
<a class="left carousel-control" href="#myCarousel" data-slide="prev">
<span class="icon-prev"></span>
</a>
<a class="right carousel-control" href="#myCarousel" data-slide="next">
<span class="icon-next"></span>
</a>
<div id="carouselButtons">
<button id="slowButton" type="button" class="btn btn-default btn-xs">
<span class="glyphicon glyphicon-play"></span>
</button>
<button id="fastButton" type="button" class="btn btn-default btn-xs">
<span class="glyphicon glyphicon-forward"></span>
</button>
</div>
</div>
哎喲,這很煩人。感謝您的解決方案。 data()從哪裏來?它似乎不是JQuery數據函數AFAICT。 – Metalskin
我不是HTML5或bootstrap方面的專家,但它似乎是用於將引導組件綁定到data-something-value屬性(如data-target =「#myCarousel」和data-slide-to =「2」)的常用名稱空間。這在轉盤示例本身中使用。這個副作用是我們可以從javascript中修改它。 –
這是一個很好的解決方案。我想添加2件事。首先,轉盤必須通過javascript(我正在使用'data-interval'屬性)的選項初始化,否則第一次調用'c.data()['bs.carousel']。options'會給出錯誤。其次,更改僅在下一次轉換時生效。爲了強制更改立即生效,可以調用'c.carousel('pause'); c.carousel('cycle');',但是這會重新啓動計時器。 – Ian