gpt4 book ai didi

javascript - 自定义轮播间隔?

转载 作者:行者123 更新时间:2023-12-01 02:05:24 25 4
gpt4 key购买 nike

在 Bootstrap 3 中,使用 jQuery 有没有一种方法可以按轮播的索引进行排序并添加每张幻灯片的自定义间隔,以便我可以让一张幻灯片 10000 毫秒,另一张幻灯片 500 毫秒等?

我知道您可以设置数据间隔属性,但这会设置所有幻灯片的速度,因为您无法向每个项目添加自定义间隔属性。

data-interval="3000"

我的轮播设置如下:

<div id="carousel" class="carousel slide">

<div class="carousel-inner">

<div class="item active">
<a href="#">
<img class="img-responsive" src="">
</a>
</div>

<div class="item">
<a href="#">
<img class="img-responsive" src="">
</a>
</div>

<div class="item">
<a href="#">
<img class="img-responsive" src="">
</a>
</div>

<div class="item">
<a href="#" >
<img class="img-responsive" src="">
</a>
</div>
</div>

最佳答案

您可以创建一个自定义属性来指示幻灯片应可见的时间长度,为 slide.bs.carouselslid.bs.carousel 上的事件项目拉出该属性 事件(无论您喜欢/最适合您),然后将其设置为超时以转到下一张幻灯片。

$('#carousel-example-generic').on('slide.bs.carousel', function() {
var interval = $('div.item.active').attr('duration');
setTimeout(function() {
$('.carousel').carousel('next');
}, interval);
});
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>


<div id="carousel-example-generic" class="carousel slide" data-ride="carousel">
<!-- Indicators -->
<ol class="carousel-indicators">
<li data-target="#carousel-example-generic" data-slide-to="0" class="active"></li>
<li data-target="#carousel-example-generic" data-slide-to="1"></li>
<li data-target="#carousel-example-generic" data-slide-to="2"></li>
<li data-target="#carousel-example-generic" data-slide-to="3"></li>
</ol>

<!-- Wrapper for slides -->
<div class="carousel-inner" role="listbox">
<div class="item active" duration="3000">
<img src="http://placehold.it/400x200" alt="..." />
<div class="carousel-caption">
First
</div>
</div>
<div class="item" duration="2000">
<img src="http://placehold.it/400x200" alt="..." />
<div class="carousel-caption">
Second
</div>
</div>
<div class="item" duration="1000">
<img src="http://placehold.it/400x200" alt="..." />
<div class="carousel-caption">
Third
</div>
</div>
<div class="item" duration="500">
<img src="http://placehold.it/400x200" alt="..." />
<div class="carousel-caption">
Fourth
</div>
</div>
</div>

<!-- Controls -->
<a class="left carousel-control" href="#carousel-example-generic" role="button" data-slide="prev">
<span class="glyphicon glyphicon-chevron-left" aria-hidden="true"></span>
<span class="sr-only">Previous</span>
</a>
<a class="right carousel-control" href="#carousel-example-generic" role="button" data-slide="next">
<span class="glyphicon glyphicon-chevron-right" aria-hidden="true"></span>
<span class="sr-only">Next</span>
</a>
</div>

关于javascript - 自定义轮播间隔?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27047620/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com