/
jquery.tabbed.js
62 lines (51 loc) · 1.4 KB
/
jquery.tabbed.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
/*!
* jQuery.tabbed() v1.0.2
* https://github.com/chovy/tabbed-navigation
*
* Copyright 2012, Anthony Ettinger
* Dual licensed under the MIT or GPL Version 2 licenses.
*
* Date: Tue Feb 21 10:22:23 PST 2012
*/
(function($){
$.fn.extend({
tabbed: function( opts ) {
//default options
var opts = $.extend({
}, opts);
var activateTab = function( tab, tabContent, tabs, tabContents ){
var url = tab.find('a').attr('href').split('#');
//make the first tab active
tabs.removeClass('active');
tab.addClass("active");
//hide all but the first tab contents
tabContents.hide();
//we have a url, do ajax call
if ( url[0] ) {
tabContent.html('');
tabContent.show();
tabContent.addClass('loading').load(url[0], function(){
tabContent.removeClass('loading');
});
} else {
tabContent.show();
}
};
return this.each(function() {
var obj = $(this);
var tabs = $('.tabs li', obj),
tabContents = $('.contents .content', obj),
firstTab = tabs.eq(0),
firstContent = tabContents.eq(0);
activateTab(firstTab, firstContent, tabs, tabContents);
//handle the tab clicks
tabs.click(function(e) {
var current = tabs.index($(this)),
currentContent = tabContents.eq(current);
e.preventDefault();
activateTab( $(this), currentContent, tabs, tabContents );
});
});
}
});
})(jQuery);