Created
February 22, 2012 21:13
-
-
Save aeurielesn/1887310 to your computer and use it in GitHub Desktop.
Modified jquery tab plugin to allow subtabs
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
$(".tabs").tabs(); |
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
(function($) { | |
var defaults = { | |
tabs: "div.tab", | |
expire: null | |
}; | |
$.fn.tabs = function(options) { | |
var counter = 1; | |
var tabSave = {}; | |
var hashStr = window.location.hash.replace('#', ''); | |
var args = hashStr.split(';'); | |
for (var val in args) { | |
if (args[val].replace(/s\g/, '') != '') { | |
var vals = args[val].split('='); | |
tabSave[vals[0]] = vals[1]; | |
} | |
} | |
function saveTabs() { | |
var str = ''; | |
for (var prop in tabSave) { | |
str += prop + '=' + tabSave[prop] + ';'; | |
} | |
window.location.hash = str; | |
} | |
function getTabs(tab) { | |
var str = window.location.hash; | |
var params = str.split(';'); | |
for (var val in params) { | |
var vals = params[val].split('='); | |
if (vals[0].replace('#', '') == tab) return vals[1]; | |
} | |
return false; | |
} | |
return this.each(function() { | |
var opts = $.extend({}, defaults, options); | |
var tis = $(this); | |
var tabs = $(this).find(opts.tabs); | |
var hash = getTabs("tab_" + counter); | |
if (hash) { | |
tabs.hide(); | |
tis.find('div.tab:eq(' + hash + ')').show(); | |
tis.find('> ul.tabmenu li:eq(' + hash + ')').addClass('selected'); | |
} else { | |
tabs.hide().filter('div:first').show(); | |
tis.find('> ul.tabmenu li:eq(0)').addClass('selected'); | |
} | |
tis.find('> ul.tabmenu a').click(function(e) { | |
tabs.hide(); | |
tabs.filter(tabs.eq($(this).parent().index())).show(); | |
tis.find('> ul.tabmenu li').removeClass('selected'); | |
$(this).parent().addClass('selected'); | |
tabSave["tab_" + tis.index()] = $(this).parent().index(); | |
saveTabs(); | |
e.preventDefault(); | |
}); | |
counter++; | |
}); | |
}; | |
})(jQuery); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment