Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
735 views
in Technique[技术] by (71.8m points)

jquery - Bootstrap Carousel Number active icon

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 the class "active" is added by default to slide 1 which then changes as the active slide changes. what i want to do is have this happen for the bullets too. i have made the first one active but want the second one to become active if the slide itself is on slide 2, etc etc

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...