如何在初始化后更改自举轮播的间隔

时间:2015-05-16 01:58:21

标签: javascript twitter-bootstrap-3 carousel

根据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>

3 个答案:

答案 0 :(得分:10)

初始化轮播后,修改速度不是受支持的选项。但是,这并不意味着它无法完成。下面是一些示例代码,使您可以动态更改选项,包括间隔

$(function () {
    $('#homeCarousel').carousel({
        interval:2000,
        pause: "false"
    });
    $('#slowButton').click(function () {
      c = $('#homeCarousel')
      opt = c.data()['bs.carousel'].options
      opt.interval= 10000;
      c.data({options: opt})
    });
    $('#fastButton').click(function () {
      c = $('#homeCarousel')
      opt = c.data()['bs.carousel'].options
      opt.interval= 1000;
      c.data({options: opt})
    });
});
#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>

答案 1 :(得分:2)

使用 Bootstrap 4 carousel ,您可以获得对其内部配置的引用,并直接设置如下所示的间隔:

const options = $("#myCcarousel").data()['bs.carousel']["_config"];
options.interval = 50;

毋庸置疑,这很糟糕。

答案 2 :(得分:-1)

如果你使用bootstrap 5,你可以使用data-bs-interval属性,就像这样

<div id="carouselExampleIndicators" class="carousel slide" data-bs-ride="carousel" data-bs-interval="10000">

这里我将间隔设置为 10 秒,因此它比默认的 bootstrap 5 间隔(即 5 秒)长 2 倍