Bootstrap Carousel Number active icon

后端 未结 2 1918
不思量自难忘°
不思量自难忘° 2020-12-17 23:08

I have been able to implement the Bootstrap carousel complete with icons to represent slides 1-3 in this example: http://jsfiddle.net/alexmoss/QzVq8/

I notice that t

相关标签:
2条回答
  • 2020-12-17 23:25

    Basically worked out the index of the currently shown slide, then used that index to apply the 'active' class to the respective navigation button.

    $('#myCarousel').bind('slid', function() {
        // Get currently selected item
        var item = $('#myCarousel .carousel-inner .item.active');
    
        // Deactivate all nav links
        $('#carousel-nav a').removeClass('active');
    
        // Index is 1-based, use this to activate the nav link based on slide
        var index = item.index() + 1;
        $('#carousel-nav a:nth-child(' + index + ')').addClass('active');
    });
    

    You can clearly optimise the code to use less variables. However, I've deliberately expanded it so that you understand what's going on.

    0 讨论(0)
  • 2020-12-17 23:38

    You need to bind 'slid' event of carousel. More details are here --> How to attach "slide" & "slid" events to the Bootstrap toolkit's carousel?

    0 讨论(0)
提交回复
热议问题