Bootstrap js Carousel Caption问题

时间:2016-10-12 14:26:42

标签: javascript jquery twitter-bootstrap carousel

我有以下旋转木马html:

<div class="carousel-wrap">
   <div class="carousel carousel-images" data-ride="carousel">
      <div class="carousel-inner" role="listbox">
         <div class="obj active"><img src="https://test.myserver.com/1.jpg"></div>
         <div class="obj"><img src="https://test.myserver.com/2.jpg"></div>
         <div class="obj"><img src="https://test.myserver.com/3.jpg"></div>
         <div class="obj"><img src="https://test.myserver.com/4.jpg"></div>
         <div class="obj"><img src="https://test.myserver.com/5.jpg"></div>
         <div class="obj"><img src="https://test.myserver.com/6.jpg"></div>
         <div class="obj"><img src="https://test.myserver.com/7.jpg"></div>
         <div class="obj"><img src="https://test.myserver.com/8.jpg"></div>
      </div>
   </div>
   <div class="carousel slide carousel-captions" data-ride="carousel" data-interval="false" data-wrap="true">
      <div class="carousel-inner" role="listbox">
         <div class="obj active">
            <div class="para">CAPTION 1</div>
         </div>
         <div class="obj">
            <div class="para">CAPTION 2</div>
         </div>
         <div class="obj">
            <div class="para">CAPTION 3</div>
         </div>
         <div class="obj">
            <div class="para"></div>
         </div>
         <div class="obj">
            <div class="para"></div>
         </div>
         <div class="obj">
            <div class="para"></div>
         </div>
         <div class="obj">
            <div class="para">CAPTION SECOND LAST</div>
         </div>
         <div class="obj">
            <div class="para">CAPTION LAST</div>
         </div>
      </div>
   </div>
   <div class="carousel-indicators-wrapper">
      <ol class="carousel-indicators">
         <li class="active"></li>
         <li class=""></li>
         <li class=""></li>
         <li class=""></li>
         <li class=""></li>
         <li class=""></li>
         <li class=""></li>
         <li class=""></li>
      </ol>
   </div>
</div>

现在,

bootstrap.js轮播中的问题:

  
    

我在旋转木马的第一张图片上有标题&#34;标题1&#34;。当我点击最后一个指示符时,会显示最后一个图像,但显示的标题是&#34;标题2&#34;。         =&GT;修正了这一点没问题

         

我在Carousel的最后一张图片上。当我点击第二个指标时,而不是&#34; CAPTION LAST&#34;向右滚动,&#34;标题1&#34;在点击后显示,并滚动到右边,&#34; CAPTION 2&#34;然后以错误的方向显示。这只发生在回去时        =&GT;无法修复/控制此事件。我为事件添加了脚本:

  
$('div.carousel')('slide.bs.carousel', function (event) {
   event.preventDefault();
   ..............
 })
  
    

但是bootstrap脚本在此之后运行并覆盖我的更改。

  

我试过了:

 $('div.carousel-indicators-wrapper ol li').click(function(event){
          event.preventDefault();
          index = $( "div.carousel-indicators-wrapper ol li" ).index( this);
          console.log('From : ' + prev_index + '===> To : ' + index);
          prev_index = index;
          var current_index = index+1;

        /*  $('div.carousel-indicators-wrapper ol li').each(function( loop_index ) {
                console.log( index );
                var il = loop_index+1;
                if(index == loop_index) {
                   $(this).addClass('active');
                   $('div.carousel.carousel-images div.item:nth-child(' + il + ')').addClass('active');
                   $('div.carousel.carousel-captions div.item:nth-child(' + il + ')').addClass('active');
                } else {
                   $(this).removeClass('active');
                   $('div.carousel.carousel-images div.item:nth-child(' + il + ')').removeClass('active');
                   $('div.carousel.carousel-captions div.item:nth-child(' + il + ')').removeClass('active');
                }

          }); */

          $('div.carousel.carousel-images div.item').removeClass('active');
          $('div.carousel.carousel-captions div.item').removeClass('active');
          $('div.carousel-indicators-wrapper ol li').removeClass('active');

          $('div.carousel.carousel-images div.item:nth-child(' + current_index + ')').addClass('active');
          $('div.carousel.carousel-captions div.item:nth-child(' + current_index + ')').addClass('active');
          $('div.carousel-indicators-wrapper ol li:nth-child(' + current_index + ')').addClass('active');

          if(index < prev_index) {
             $('div.carousel.carousel-images div.item:nth-child(' + current_index + ')').addClass('right');
             $('div.carousel.carousel-captions div.item:nth-child(' + current_index + ')').addClass('right');
          } else if(index > prev_index) {
             $('div.carousel.carousel-images div.item:nth-child(' + current_index + ')').addClass('left');
             $('div.carousel.carousel-captions div.item:nth-child(' + current_index + ')').addClass('left');
          }

     });

在botstrap.js第376行中,添加了对所有函数定义的警报,

Carousel.prototype.to = function (pos) {
    alert('prototype.to');
    .......
  }

Carousel.prototype.slide = function (pos) {
    alert('prototype.slide');
    .......
  }
  
    

警报(&#39; prototype.to&#39);被称为第一。但点击指标并获得&#34; prototype.to&#34;警报,&#34;标题1&#34;被标记为活跃。然后滚动,而不是&#34; Caption Last&#34;

  

单击指示器时bootstrap.js轮播中触发了哪个事件以及如何覆盖它?

1 个答案:

答案 0 :(得分:0)

我能够解决这个问题:

$(document).ready(function() {

     var index = 0;
     var prev = 0;
     var indicator_click = false;
     $('div.carousel-indicators-wrapper ol.carousel-indicators li').click(function(event){
        index = $(this).index();
        indicator_click = true;
     });

     $('div.carousel').bind('slide.bs.carousel', function (e) {

        if(indicator_click) {
          $('div.carousel-captions div.item').removeClass('active');
          if(index < prev){
           $('div.carousel-captions div.item:eq(' + prev + ')').addClass('right');
           $('div.carousel-captions div.item:eq(' + prev + ')').addClass('active');
           $('div.carousel-captions div.item:eq(' + index + ')').addClass('left');
          } else {
           var i = index;
           var count = $('div.carousel-captions div.carousel-inner').children().length;
           if(index < 0) {
               i = count -1;
           } else {
               i = index - 1;
           }
           $('div.carousel-captions div.carousel-inner div.item:eq(' + i + ')').addClass('active');
          }
        }

     });

      $('div.carousel').bind('slid.bs.carousel', function (e) {
           $('div.carousel-captions div.item').removeClass('left');
           $('div.carousel-captions div.item').removeClass('right');
           prev = index;
           indicator_click = false;
      });
});