-
Notifications
You must be signed in to change notification settings - Fork 0
/
willtabs.js
109 lines (108 loc) · 3.3 KB
/
willtabs.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
/* Willtabs!
* Simple tab JS
* Published under MIT License
* Copyright (c) 2013 Will Broderick
*/
(function($){
$.fn.willtabs = function(params){
//Defaults
var settings = {
tabTextColour: '#333',
tabBG: '#f5f5f5',
activeTabTextColour: '#333',
activeTabBG: '#fff',
borderStyle: '1px solid #aaa',
tabTopMargin: '20px',
contentPadding: '10px 0',
tabButtonPaddingTopBottom: 5,
tabButtonPaddingLeftRight: 20,
activeTabVerticalOffset: 6,
borderRadius: 2
};
$.extend(settings, params);
//Find tab groups
$(this).each(function(){
if($(this).hasClass('.willtab-container')) {
var $toProcess = $(this);
} else {
var $toProcess = $(this).find('.willtab-container');
}
$toProcess.each(function(){
//Handy vars
var $tabs = $(this).children('.willtab-tabs');
var $content = $(this).children('.willtab-content');
//Styles
$tabs.css({
'margin': settings.tabTopMargin + ' 0 0 0',
'display': 'block',
'border-bottom': settings.borderStyle
}).bind('checkstyle', function(){
var $lis = $(this).find('li');
$lis.find('a').css({
'float': 'left',
'border-top': settings.borderStyle,
'border-right': settings.borderStyle,
'border-left': settings.borderStyle,
'border-bottom': 'none',
'background': settings.tabBG,
'padding': settings.tabButtonPaddingTopBottom + 'px ' + settings.tabButtonPaddingLeftRight + 'px',
'margin': settings.activeTabVerticalOffset + 'px 0 0',
'text-decoration': 'none',
'color': settings.tabTextColour,
'border-top-left-radius': settings.borderRadius,
'border-top-right-radius': settings.borderRadius,
'position': 'relative',
'top': 0
}).each(function(index){
if(index > 0) {
//For all but first, nudge over to the left 1px to make a nice overlap
$(this).css('margin-left', '-1px');
}
});
$lis.filter('.active').find('a').css({
'color': settings.activeTabTextColour,
'background': settings.activeTabBG,
'padding-top': settings.tabButtonPaddingTopBottom + settings.activeTabVerticalOffset - 1,
'padding-bottom': settings.tabButtonPaddingTopBottom + 1,
'margin-top': '0',
'top': 1
});
});
$tabs.find('li').css({
'float': 'left',
'list-style': 'none',
'margin': '0'
});
//Add clearing-li onto end (the simplest of the many choices of hacks)
$('<li />').css({
'list-style': 'none',
'clear': 'both',
'height': 0
}).appendTo($tabs);
//Content style
$content.css({
'clear': 'both',
'padding': settings.contentPadding
});
//Set titles when hidden
$tabs.find('a[data-title]').each(function(){
$(this).html($(this).attr('data-title'));
});
//Set events
$tabs.find('a').bind('click', function(){
$content.children('.tab-' + $(this).attr('data-tab')).show().siblings().hide();
$tabs.children('.active').removeClass('active');
$(this).parent().addClass('active');
$tabs.trigger('checkstyle');
return false;
});
//Select first tab
$tabs.find('a:first').trigger('click');
});
});
};
$(function(){
//DOM ready? Go!
$('body').willtabs();
});
}(jQuery));