Created
August 7, 2012 15:52
-
-
Save hereswhatidid/3286576 to your computer and use it in GitHub Desktop.
Create Tabs from divs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // | |
| // 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