如何在初始化后更改自举轮播的间隔 [英] How to change interval of a bootstrap carousel once it has been initialised

查看:73
本文介绍了如何在初始化后更改自举轮播的间隔的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据doco,使用以下内容将设置轮播周期速度:

According to the doco, using the following will set the carousel cycle speed:

$('.carousel').carousel({
  interval: 2000
})

但是如果你已经初始化轮播,以不同的间隔调用上面的内容没有任何效果。

However if you have already initialised the carousel, calling the above with a different interval has no effect.

我应该注意,通过JS初始化轮播不会设置轮播上的数据间隔。我也在这个主题上搜索了很多,但结果都是关于人们试图以固定速度进行设置。我想在初始化后改变速度。

I should note that initialising the carousel via JS doesn't set the data-interval on the carousel. I've also searched quite a bit on this topic, but the results are all about people trying to set up at a fixed speed. I want to change the speed once it's already been initialised.

代码如下:

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

推荐答案

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

It's not a supported option to modify speed once the carousel is initialized. However, that doesn't mean that it cannot be done. Here is some sample code that enables you to change the options on the fly, including the interval

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

这篇关于如何在初始化后更改自举轮播的间隔的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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