Skip to content

Instantly share code, notes, and snippets.

@felipelavinz
Created January 5, 2011 22:14
Show Gist options
  • Save felipelavinz/767126 to your computer and use it in GitHub Desktop.
Save felipelavinz/767126 to your computer and use it in GitHub Desktop.
Tabs navigation for jQuery
jQuery.fn.tab_nav = function(settings) {
settings = jQuery.extend({
active_class: 'active',
return_act : false
},settings);
var s = settings,
$el = jQuery(this),
$tab_links = $el.find('a'),
$is_active = $el.find('.'+s.active_class);
// Set first tab as active on document ready
if ( $is_active.length ) {
for ( var i = 0, len = $tab_links.length; i < len; i++ ){
jQuery('#' + $tab_links[i].href.split('#')[1] ).hide();
}
jQuery('#'+ $is_active.find('a').attr('href').split('#')[1] ).show();
} else {
jQuery($tab_links[0]).closest('li').addClass(s.active_class);
for ( var i = 1, len = $tab_links.length; i < len; i++ ){
jQuery('#' + $tab_links[i].href.split('#')[1] ).hide();
}
}
// Event detection and delegation
jQuery(this).delegate('a', 'click', function(){
var $oldActive = $el.find('.'+s.active_class);
jQuery('#'+ $oldActive.find('a').attr('href').split('#')[1] ).hide();
$oldActive.removeClass(s.active_class);
jQuery(this).closest('li').addClass(s.active_class);
jQuery('#'+ jQuery(this).attr('href').split('#')[1] ).show();
return false;
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment