Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
188
rated 0 times [  192] [ 4]  / answers: 1 / hits: 16111  / 10 Years ago, mon, february 24, 2014, 12:00:00

I have a Twitter Bootstrap 3 carousel without indicator buttons.
Instead of those indicator buttons, I use tabs (which I style accordingly in my CSS).
This way, the user has an idea what each slide is (based on the tab label), so it's easier for him to go to the slide he's interested in.



whatIsCarousel



The HTML looks like this:



<div class=carousel slide data-ride=carousel id=whatIsCarousel>
<div class=carousel-inner>
<div class=item active>
<img src=slide0.png>
</div>
<div class=item>
<img src=slide1.png>
</div>
...
</div>
<a class=left carousel-control data-slide=prev href=#whatIsCarousel>
<span class=glyphicon glyphicon-chevron-left></span>
</a>
<a class=right carousel-control data-slide=next href=#whatIsCarousel>
<span class=glyphicon glyphicon-chevron-right></span>
</a>
</div>
<ul class=nav nav-justified id=whatIsCarouselButtons>
<li class=active data-target=#whatIsCarousel data-slide-to=0>
<a data-toggle=tab href=#>Slide 0</a>
</li>
<li data-target=#whatIsCarousel data-slide-to=1>
<a data-toggle=tab href=#>Slide 1</a>
</li>
...
</ul>


The CSS is irrelevant, but source code is here.



The tabs works well: When I click on one of the tabs, the carousel slides to the appropriate slide, and the correct tab becomes active (highlights).



The carousel controls (left and right) work partially: when I click on one of the controls (or it slides automatically), the carousel slides to the appropriate slide, but the active tab does not change. How do I make the active tab change when the carousel slides?



Here's what I got so far in my javascript:



$(document).ready( function() {
$carousel.carousel();
$('#whatIsCarousel').on('slide.bs.carousel', function(e) {
var from = active.index();
var next = $(event.relatedTarget);
var to = next.index();

// Is to the index of the next slide?
// How do I find tab that needs to be active next?
});
});

More From » jquery

 Answers
36

I hope this way could help you :



Bootply : http://www.bootply.com/116312



Js :



$(document).ready( function() {
$('.carousel').carousel();
$('#whatIsCarousel').on('slide.bs.carousel', function(e) {
var from = $('.nav li.active').index();
var next = $(e.relatedTarget);
var to = next.index();

// This could interest you
$('.nav li').removeClass('active').eq(to).addClass('active');

});
});


Focus on :



$('.nav li').removeClass('active').eq(to).addClass('active');


You remove active class from all tabs, and add it to the tabs that has the same index.


[#72348] Saturday, February 22, 2014, 10 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
jenamackennac

Total Points: 304
Total Questions: 110
Total Answers: 107

Location: Ecuador
Member since Thu, Jun 4, 2020
4 Years ago
jenamackennac questions
Fri, Feb 18, 22, 00:00, 2 Years ago
Wed, Apr 21, 21, 00:00, 3 Years ago
Thu, Apr 1, 21, 00:00, 3 Years ago
Tue, Feb 2, 21, 00:00, 3 Years ago
;