如何设置每个轮播项目的间隔持续时间?

时间:2017-04-18 06:52:44

标签: javascript jquery html css twitter-bootstrap

首先,这不是任何帖子的重复,我有自举轮播,我试图为每个引导轮播.item设置间隔,我找到了它的代码from here但是这个例子我的代码片段不起作用我的错误在哪里?我的代码与with this carousel相同,但没有发生任何事情我怎样才能为每个bootstrap轮播项设置间隔?

目前这个每个轮播只能工作,我认为它应该适用于每个幻灯片(默认覆盖)有两个原因:

可以设置第一张幻灯片以显示快速概览等 有些幻灯片可能有更多的文字,需要更长的阅读延迟,有些可能更少...

E.g

<div class="carousel-inner">
    <div class="active item" data-interval="500">…</div>
    <div class="item" data-interval="5000">…</div>
    <div class="item" data-interval="5000>…</div>
</div>

我的真实例子

&#13;
&#13;
$(window).load(function() {
  var t;

  var start = $('#carousel').find('.active').attr('data-interval');
  t = setTimeout("$('#carousel').carousel({interval: 1000});", start - 1000);

  $('#carousel').on('slid.bs.carousel', function() {
    clearTimeout(t);
    var duration = $(this).find('.active').attr('data-interval');

    $('#carousel').carousel('pause');
    t = setTimeout("$('#carousel').carousel();", duration - 1000);
  })
  $('.ani-icon-right-chevron').on('click', function() {
    clearTimeout(t);
  });

  $('.ani-icon-chevron-pointing-to-the-left').on('click', function() {
    clearTimeout(t);
  });

});
&#13;
#carousel {
  width: 700px;
  margin: 70px auto;
}
&#13;
<link href="//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css" rel="stylesheet" />



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

  <div class="carousel-inner">
    <div class="item active" data-interval="300">
      <a href="#"><img src="http://placekitten.com/1600/600" /></a>
      <div class="carousel-caption">
        <h3>Meow</h3>
        <p>Just Kitten Around</p>
      </div>
    </div>
    <div class="item" data-interval="600">
      <a href="#"><img src="http://placekitten.com/1600/600" /></a>
      <div class="carousel-caption">
        <h3>Meow</h3>
        <p>Just Kitten Around</p>
      </div>
    </div>
    <div class="item" data-interval="900">
      <a href="#"><img src="http://placekitten.com/1600/600" /></a>
      <div class="carousel-caption">
        <h3>Meow</h3>
        <p>Just Kitten Around</p>
      </div>
    </div>
  </div>

  <a class="left carousel-control" href="#carousel" data-slide="prev">
    <span class="glyphicon glyphicon-chevron-left"></span>
  </a>
  <a class="right carousel-control" href="#carousel" data-slide="next">
    <span class="glyphicon glyphicon-chevron-right"></span>
  </a>
</div>

<script src="//cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script src="//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/js/bootstrap.min.js"></script>
&#13;
&#13;
&#13;

3 个答案:

答案 0 :(得分:1)

编辑了你的代码,到目前为止我的工作已经完成了

&#13;
&#13;
<link href="//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css" rel="stylesheet" />



<div id="carousel" class="carousel slide" data-ride="carousel" data-interval="1000" >
    <ol class="carousel-indicators">
        <li data-target="#carousel" data-slide-to="0" class="active"></li>
        <li data-target="#carousel" data-slide-to="1"></li>
        <li data-target="#carousel" data-slide-to="2"></li>
    </ol>

    <div class="carousel-inner">
        <div class="item active" interval="1000">
            <a href="#"><img src="http://placekitten.com/1600/600" /></a>
            <div class="carousel-caption">
                <h3>Meow</h3>
                <p>Just Kitten Around</p>
            </div>
        </div>
        <div class="item" interval="10000">
            <a href="#"><img src="http://placekitten.com/1600/600" /></a>
            <div class="carousel-caption">
                <h3>Meow</h3>
                <p>Just Kitten Around</p>
            </div>
        </div>
        <div class="item" interval="5000">
            <a href="#"><img src="http://placekitten.com/1600/600" /></a>
            <div class="carousel-caption">
                <h3>Meow</h3>
                <p>Just Kitten Around</p>
            </div>
        </div>
    </div>

    <a class="left carousel-control" href="#carousel" data-slide="prev">
        <span class="glyphicon glyphicon-chevron-left"></span>
    </a>
    <a class="right carousel-control" href="#carousel" data-slide="next">
        <span class="glyphicon glyphicon-chevron-right"></span>
    </a>
</div>

<script src="//cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script src="//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/js/bootstrap.min.js"></script>

<script>
    $(window).load(function () {
        var st;

        $('#carousel').on('slid.bs.carousel', function () {
         //   debugger;

            var newinterval = $('#carousel .carousel-inner').find('.active').attr('interval');

            $('#carousel').carousel("pause");
            clearTimeout(st);
           st =  setTimeout("$('#carousel').carousel()", newinterval);

           // $('#carousel').carousel({ interval: newinterval });
        });
    });
</script>
&#13;
&#13;
&#13;

答案 1 :(得分:0)

尝试将data-interval="value"添加到<div id="carousel" class="carousel slide" data-ride="carousel">

答案 2 :(得分:0)

<link href="//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css" rel="stylesheet" />



<div id="carousel" class="carousel slide" data-ride="carousel" data-interval="1000" >
    <ol class="carousel-indicators">
        <li data-target="#carousel" data-slide-to="0" class="active"></li>
        <li data-target="#carousel" data-slide-to="1"></li>
        <li data-target="#carousel" data-slide-to="2"></li>
    </ol>

    <div class="carousel-inner">
        <div class="item active">
            <a href="#"><img src="http://placekitten.com/1600/600" /></a>
            <div class="carousel-caption">
                <h3>Meow</h3>
                <p>Just Kitten Around</p>
            </div>
        </div>
        <div class="item">
            <a href="#"><img src="http://placekitten.com/1600/600" /></a>
            <div class="carousel-caption">
                <h3>Meow</h3>
                <p>Just Kitten Around</p>
            </div>
        </div>
        <div class="item">
            <a href="#"><img src="http://placekitten.com/1600/600" /></a>
            <div class="carousel-caption">
                <h3>Meow</h3>
                <p>Just Kitten Around</p>
            </div>
        </div>
    </div>

    <a class="left carousel-control" href="#carousel" data-slide="prev">
        <span class="glyphicon glyphicon-chevron-left"></span>
    </a>
    <a class="right carousel-control" href="#carousel" data-slide="next">
        <span class="glyphicon glyphicon-chevron-right"></span>
    </a>
</div>