简体   繁体   中英

Bootstrap js Carousel Caption issue

I have following 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>

Now,

Issues in bootstrap.js carousel :

I am on 1st image of carousel with caption "CAPTION 1". When I click last indicator, the last image is shown, but caption shown is "CAPTION 2". => Fixed this. So no issue

I am on Last image of Carousel. When I click 2nd indicator, instead of "CAPTION LAST" scrolling to right, "CAPTION 1" is shown after that click, and is scrolled to right, and "CAPTION 2" is then shown and in WRONG DIRECTION. This only happens while going back => Unable to fix/control this event. I added script for event :

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

But bootstrap script runs after this and overrides my changes.

I have tried :

 $('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');
          }

     });

and

In botstrap.js line 376, added alerts to all function definitions,

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

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

alert('prototype.to'); was called first. But after clicking the indicator and getting "prototype.to" alert, the "CAPTION 1" was marked active. Which then is scrolled, instead of "Caption Last"

Which event is triggered in bootstrap.js carousel on clicking an indicator and how to override it?

I was able to resolve the issue :

$(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;
      });
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM