Skip to content

Instantly share code, notes, and snippets.

@hereswhatidid
Created August 7, 2012 15:52
Show Gist options
  • Select an option

  • Save hereswhatidid/3286576 to your computer and use it in GitHub Desktop.

Select an option

Save hereswhatidid/3286576 to your computer and use it in GitHub Desktop.
Create Tabs from divs
//
// Tabbed containers
//
// -----------------------------------------------------
if ($('.tab').length > 1) {
var tabnav = $('<div class="tabs-nav"><ul /></div>'),
tabs = $('<div class="tabs" />'),
tabobject = $('<div class="tabbed-content span9" />'),
activefound = false,
tabwidth = 100 / $('.tab').length;
tabholder = $(tabobject).insertBefore($('.tab:first'));
$('.tab').each(function(index) {
var title = $('.title', this).text(),
tabid = 'tabid-' + index;
$(this).addClass(tabid).removeClass('span9');
$('.title', this).hide();
if ($(this).hasClass('active')) {
activefound = true;
$('ul', tabnav).append('<li><a href="#' + tabid + '" data-tabclass="' + tabid + '" class="active">' + title + '</a></li>');
} else {
$(this).hide();
$('ul', tabnav).append('<li><a href="#' + tabid + '" data-tabclass="' + tabid + '">' + title + '</a></li>');
}
tabs.append($(this));
});
if (!activefound) {
$('.tab:first', tabs).show();
$('a:first', tabnav).addClass('active');
}
// $('li', tabnav).css({
// 'width': tabwidth + '%'
// })
$('a', tabnav).on('click', function(e) {
e.preventDefault();
var parent = $(this).closest('.tabbed-content'),
curtab = $('.tab:visible', curtab);
if ($(this).hasClass('active')) return false;
$('.tabs-nav a', parent).removeClass('active');
$(this).addClass('active');
$('.' + $(this).data('tabclass'), parent).fadeIn(200);
curtab.hide();
});
tabholder.append(tabnav);
tabholder.append(tabs);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment