自定义轮播间隔? [英] Custom Carousel Intervals?

查看:110
本文介绍了自定义轮播间隔?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

In Bootstrap 3, with jQuery is there a way to sort by the index of my carousel and add custom intervals of each slide so that I can have one slide 10000ms and another 500ms etc?

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

I know you can set the data-interval attribute but this sets the speed for all slides as you can't add a custom interval attribute to each item.

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.carousel slid.bs.carousel 事件(根据您的喜好/最适合您),然后将其设置为超时以转到下一张幻灯片。

You can create a custom attribute that denotes how long the slide should be visible for, pull that out for the active item on the slide.bs.carousel or slid.bs.carousel events (whichever you prefer/works best for you), then set it as a timeout to go to the next slide.

$('#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>

这篇关于自定义轮播间隔?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆