Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 856
manc Avatar asked May 08 '12 13:05

manc


People also ask

How do I change my carousel indicators icon?

You just need to override two properties ( width and height ) and add a new one, border-radius , to . carousel-indicators li . Make width and height values equal eg: 10px each and give a border-radius of 100% .

How do I add a button to carousel?

Show activity on this post. You can do so by setting the carousel's position to relative and then setting then placing your button within the you want the button to appear in. After that is done, you should set the buttons position to absolute.

What is data BS interval?

Options can be passed via data attributes or JavaScript. For data attributes, append the option name to data-bs- , as in data-bs-interval="" . The amount of time to delay between automatically cycling an item.


2 Answers

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.

like image 119
Dan Harrison Avatar answered Sep 21 '22 11:09

Dan Harrison


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

like image 22
rt2800 Avatar answered Sep 19 '22 11:09

rt2800