javascript - Next-Previous button to work in loop using Bootstrap Tab -
currently "previous" button doesn't work in first tab since it's not in loop. how should go setting when visitors click on previous, shows last tab?
html:
<div> <ul id="wheel-tab" class="nav nav-tabs nav-justified"> <li class="active"><a href="#ocean" data-toggle="tab">ocean</a></li> <li><a href="#air" data-toggle="tab">air</a></li> <li><a href="#customs" data-toggle="tab">customs</a></li> <li><a href="#transport" data-toggle="tab">transport</a></li> </ul> <div class="tab-content"> <div id="ocean" class="tab-pane fade in active"> <h2>ocean</h2> <p>lorem ipsum dolor sit amet, consectetur adipisicing elit, sed eiusmod tempor incididunt ut labore et dolore magna aliqua.</p> </div> <div id="air" class="tab-pane fade in active"> <h2>air</h2> <p>lorem ipsum dolor sit amet, consectetur adipisicing elit, sed eiusmod tempor incididunt ut labore et dolore magna aliqua.</p> </div> <div id="customs" class="tab-pane fade in active"> <h2>customs</h2> <p>lorem ipsum dolor sit amet, consectetur adipisicing elit, sed eiusmod tempor incididunt ut labore et dolore magna aliqua.</p> </div> <div id="transport" class="tab-pane fade in active"> <h2>transport</h2> <p>lorem ipsum dolor sit amet, consectetur adipisicing elit, sed eiusmod tempor incididunt ut labore et dolore magna aliqua.</p> </div> </div> <a class="left carousel-control" href="#" id="wheel-left"role="button" data-slide="prev"> <span class="icon icon-left">prev</span> </a> <a class="right carousel-control" href="#" id="wheel-right" role="button" data-slide="prev"> <span class="icon icon-right">next</span> </a> </div>
js:
var $tabs = $('#wheel-tab li'); $('#wheel-left').on('click', function () { $tabs.filter('.active').prev('li').find('a[data-toggle="tab"]').tab('show'); }); $('#wheel-right').on('click', function () { $tabs.filter('.active').next('li').find('a[data-toggle="tab"]').tab('show'); });
you can add check of length respect prev
, next
, if present continue normal thing else, toggle last/first
tabs respectively. take @ solution below , go through comments:
$('#wheel-left').on('click', function() { var prevlen = $tabs.filter('.active').prev('li').length; //check if prev li present if (prevlen) $tabs.filter('.active').prev('li').find('a[data-toggle="tab"]').tab('show');//if yes continue normal tab switch else $tabs.filter('li:last').find('a[data-toggle="tab"]').tab('show');//else show last tab }); $('#wheel-right').on('click', function() { var nextlen = $tabs.filter('.active').next('li').length;//check if next li present if (nextlen) $tabs.filter('.active').next('li').find('a[data-toggle="tab"]').tab('show');//if yes continue normal tab switch else $tabs.filter('li:first').find('a[data-toggle="tab"]').tab('show');//else show first tab });
Comments
Post a Comment