I would like the Bootstrap tabs to cycle through each tab, kind of like a carousel, changing every 10 seconds or so. I figure it’s going to require some custom javascript (but I’m a bit of a novice)!
(I’d also like the cycle to stop if a tab has been manually clicked, but that’s more of a ‘stretch goal’!)
Here’s what I’ve got so far: http://jsfiddle.net/59Lyhqmn/31/
I’ve tried implementing solutions from similar questions, such as:
JavaScript
x
19
19
1
$(function(){
2
var tabs = $('#myTab a');
3
var counter = 0;
4
window.setInterval(activateTab, 1000);
5
function activateTab(){
6
7
tabs.removeClass('active');
8
var currentLink = tabs[counter];
9
10
$('.tab-pane').removeClass('.active').hide();
11
currentLink.addClass('active');
12
$(currentLink.attr('href')).addClass('active').show();
13
if(counter < tabs.length)
14
counter= counter + 1;
15
else
16
counter = 0;
17
}
18
});
19
but I’ve had no luck so far!
Any help would be much appreciated!
Advertisement
Answer
I saw your fiddle I came up with the solution using simple iteration JSFiddle Link Working
JavaScript
1
31
31
1
$(function(){
2
var count = 0;
3
setInterval(function(){
4
if(count == 1){
5
$("ul>li:nth-child(4)>a").removeClass("active");
6
$("ul>li:nth-child(1)>a").addClass("active");
7
$("#first-tab").addClass("active show in");
8
$("#fourth-tab").removeClass("active show in");
9
}
10
else if(count == 2){
11
$("ul>li:nth-child(2)>a").addClass("active");
12
$("ul>li:nth-child(1)>a").removeClass("active");
13
$("#second-tab").addClass("active show in");
14
$("#first-tab").removeClass("active show in");
15
}
16
else if(count == 3){
17
$("ul>li:nth-child(3)>a").addClass("active");
18
$("ul>li:nth-child(2)>a").removeClass("active");
19
$("#third-tab").addClass("active show in");
20
$("#second-tab").removeClass("active show in");
21
}
22
else if(count == 4){
23
$("ul>li:nth-child(4)>a").addClass("active");
24
$("ul>li:nth-child(3)>a").removeClass("active");
25
$("#fourth-tab").addClass("active show in");
26
$("#third-tab").removeClass("active show in");
27
}
28
if (count == 4){count=1;}else{count+=1}
29
}, 10000);
30
});
31