引导转盘是否可以仅通过部分滑轨旋转?

我直接从自助站点有一个香草旋转木马。

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

它有5个幻灯片,有5个指标:

 1 2 3 AB 

我希望旋转木马自动循环滑动1 2和3,然后重复。 但是,如果我点击幻灯片A或B的指示器,我想转到该幻灯片并暂停转盘。

如何将自动旋转限制为仅滑动1 2和3?

  1. 您需要点击幻灯片事件,在它开始之后,如果当前活动的幻灯片是应该重置滑块的幻灯片,则告诉它转到第一张幻灯片。
  2. 如果当前幻灯片事件是自动播放幻灯片或由滑块控件启动,您还需要存储,因为您希望允许控件转到不受限制的幻灯片
  3. 此外,您需要在到达循环重置点之后的任何幻灯片时暂停滑块,如果您在下方,则播放滑块(在从暂停的幻灯片返回后重新启动自动播放)。

但显然,Bootstrap轮播不喜欢从现有的幻灯片事件中启动新的幻灯片事件,所以我们需要在当前的一个上调用.preventDefault()才能启动另一个。

aPautoPlaying )变量存储当前幻灯片事件是否已通过与滑块控件的交互启动,
cRcycleReset )存储应该重置循环的幻灯片的索引
sL选择器 )应该是滑块的选择器。

这里是:

 var sL = '#z', // slider selector aP = true, // autoPlaying cR = 2; // cycleReset $(sL) .on('click', '.carousel-indicators,.carousel-control', function() { // autoplaying off when a control is clicked aP = false; }) .on('slide.bs.carousel', function(e) { // if on the cycle reset slide and auto-playing... if (($(sL + ' .active').index() == cR) && aP) { // ...we stop the current slide event... e.preventDefault(); // ...turn off autoplaying (to prevent an infinite loop)... aP = false; // ...and go to first slide; $(sL).carousel(0) } else { // or we set auto-playing true (for next slide event). aP = true; } }) .on('slid.bs.carousel', function(e) { // when a slide event finished, if we arrived on a slide // that's after the cycle reset slide, we pause the slider // otherwise we play (cycle) it $(sL).carousel($(sL + ' .active').index() > cR ? 'pause' : 'cycle'); }); 

工作范例:

 var sL = '#z', aP = true, cR = 2; $(sL) .on('click', '.carousel-indicators,.carousel-control', function() { aP = false; }) .on('slide.bs.carousel', function(e) { if (($(sL + ' .active').index() == cR) && aP) { e.preventDefault(); aP = false; $(sL).carousel(0) } else { aP = true; } }) .on('slid.bs.carousel', function(e) { $(sL).carousel($(sL + ' .active').index() > cR ? 'pause' : 'cycle'); }); 
 body {margin: 0;} .item img { width: 100%; height: auto; }