How to Get total and Current Slide Number of Carousel

前端 未结 7 1429
野的像风
野的像风 2020-12-07 20:58

Can you please let me know how I can get the total and current number of the carousel slides in bootstrap like the image below?

7条回答
  •  渐次进展
    2020-12-07 21:36

    The only drawback with the below code will be that it doesn't show up till you go to next slide (triggers after each slide change).

    JS/jQuery

    $('#carousel-slide').on('slid.bs.carousel', function () {
      var carouselData = $(this).data('bs.carousel');
      var currentIndex = carouselData.getActiveIndex();
      var total = carouselData.$items.length;
    
      var text = (currentIndex + 1) + " of " + total;
      $('#carousel-index').text(text);
    });
    

    HTML

    
    

提交回复
热议问题