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

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何在初始化后更改自举轮播的间隔相关的知识,希望对你有一定的参考价值。

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

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


$(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>
另一答案

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

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

毋庸置疑,这是非常hacky。

以上是关于如何在初始化后更改自举轮播的间隔的主要内容,如果未能解决你的问题,请参考以下文章

关于jquery轮播的问题,如何通过1234标号来控制轮播?

如何获取轮播的总数和当前幻灯片数

根据用户在轮播中的位置查找猫头鹰轮播的索引

无缝轮播图的一种方式原理

图片轮播的手写代码

如何调整 Bootstrap 轮播的 CSS 响应性