How to Get total and Current Slide Number of Carousel

前端 未结 7 1411
野的像风
野的像风 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:15

    Each slide has a .item class to it, you can get the total number of slides like this

    var totalItems = $('.item').length;
    

    Active slide has a class named as active, you can get the index of active slide like this

    var currentIndex = $('div.active').index() + 1;
    

    You can update these values by binding the bootstrap carousel slid event like this

    $('#myCarousel').bind('slid', function() {
        currentIndex = $('div.active').index() + 1;
       $('.num').html(''+currentIndex+'/'+totalItems+'');
    });
    

    EXAMPLE

提交回复
热议问题