-
Notifications
You must be signed in to change notification settings - Fork 0
/
jquery.smartTabs.js
197 lines (172 loc) · 6.36 KB
/
jquery.smartTabs.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
/*
__ ______ __ _
_________ ___ ____ ______/ //_ __/___ _/ /_ _____ (_)____
/ ___/ __ `__ \/ __ `/ ___/ __// / / __ `/ __ \/ ___/ / / ___/
(__ ) / / / / / /_/ / / / /_ / / / /_/ / /_/ (__ ) / (__ )
/____/_/ /_/ /_/\__,_/_/ \__//_/ \__,_/_.___/____(_)_/ /____/
/___/
A jQuery plug-in to control tab/accordion functions primarily for responsive sites.
cornerstoneUX.smartTabs Plug-In
Version: 2.0.0
Author: Matt Zimmermann
Website & Documentation: http://influxweb.github.io/cornerstoneUX.smartTabs/
Repo: https://github.com/influxweb/cornerstoneUX.smartTabs
Issues: https://github.com/influxweb/cornerstoneUX.smartTabs/issues
License: MIT
*/
;(function($){
if (!$.cornerstoneUX) {
$.cornerstoneUX = {};
};
$.cornerstoneUX.smartTabs = function(el, options){
var base = this;
// Access to jQuery and DOM versions of element
base.$el = $(el);
base.el = el;
// Add a reverse reference to the DOM object
base.$el.data('cornerstoneUX.smartTabs', base);
base.viewport = $(window).outerWidth();
base.ddHeight = '';
base.init = function () {
base.options = $.extend({},$.cornerstoneUX.smartTabs.defaultOptions, options);
if ((base.viewport <= base.options.breakpoint && base.options.layout == 'auto') || base.options.layout == 'accordion') {
base.$el.addClass('accordion');
}
else if ((base.viewport > base.options.breakpoint && base.options.layout == 'auto') || base.options.layout == 'tabs') {
base.$el.addClass('tabs');
};
base.ddHeight = [];
base.$el.each(function () {
var smartTabs = $(this),
smartTabsHeight = smartTabs.find('dt').outerHeight(),
currentHeight = 0,
current,
hash = location.hash;
if (base.$el.hasClass('tabs')) {
if (base.options.contentHeight == 'fixed') {
smartTabs.find('dd').each(function (i) {
var dd = $(this);
dd.addClass('invisible');
if (dd.outerHeight() > currentHeight) {
currentHeight = dd.outerHeight();
};
dd.removeClass('invisible');
});
smartTabs.css('height', smartTabsHeight + currentHeight + 'px');
smartTabs.find('dd').css('height', currentHeight + 'px');
console.log(currentHeight);
}
else if (base.options.contentHeight == 'auto') {
smartTabs.find('dd').each(function (i) {
var dd = $(this);
dd.addClass('invisible');
base.ddHeight[i] = dd.outerHeight();
dd.removeClass('invisible');
});
};
smartTabs.find('dd').css('top', smartTabsHeight + 'px');
}
else {
smartTabs.css('height', 'auto');
smartTabs.find('dd').css({
height: 'auto',
top: 0
});
};
smartTabs.find('dt').removeClass('current');
smartTabs.find('dd').hide();
if (smartTabs.find('dt a[href="' + hash + '"]').length) {
if (base.$el.hasClass('tabs') && base.options.contentHeight == 'auto') {
var findCurrent = smartTabs.find('a[href="' + hash + '"]').parent().addClass('current').next('dd'),
i = findCurrent.index('dd');
smartTabs.css('height', smartTabsHeight + base.ddHeight[i] + 'px');
current = findCurrent.css('height', base.ddHeight[i] + 'px').show();
}
else {
current = smartTabs.find('a[href="' + hash + '"]').parent().addClass('current').next('dd').show();
};
}
else {
if (base.$el.hasClass('tabs') && base.options.contentHeight == 'auto') {
var findCurrent = smartTabs.find('dt:first').addClass('current').next('dd'),
i = findCurrent.index('dd');
smartTabs.css('height', smartTabsHeight + base.ddHeight[i] + 'px');
current = findCurrent.css('height', base.ddHeight[i] + 'px').show();
}
else {
current = smartTabs.find('dt:first').addClass('current').next('dd').show();
};
};
});
};
base.navigation = function () {
base.$el.on('click', 'dt a', function (e) {
var smartTabs = $(this),
smartTabsHeight = smartTabs.parent('dt').outerHeight();
e.stopPropagation();
e.preventDefault();
e.stopImmediatePropagation();
if (base.$el.hasClass('accordion')) {
if (smartTabs.parent('dt').hasClass('current')) {
var current = smartTabs.parent('dt').removeClass('current').next('dd').slideUp(300);
}
else {
smartTabs.parents('dl').find('.current').removeClass('current').next('dd').hide();
var current = smartTabs.parent('dt').addClass('current').next('dd').slideDown(300);
current.animate({scrollTop: -smartTabs.parent('dt').outerHeight()}, 800);
};
}
else {
if (!smartTabs.parent('dt').hasClass('current')) {
if (base.options.contentHeight == 'auto') {
smartTabs.parents('dl').find('.current').removeClass('current').next('dd').hide();
var findCurrent = smartTabs.parent('dt').addClass('current').next('dd'),
i = findCurrent.index('dd'),
current = findCurrent.css('height', base.ddHeight[i] + 'px').fadeIn();
smartTabs.parents('dl').css('height', smartTabsHeight + base.ddHeight[i] + 'px');
}
else {
smartTabs.parents('dl').find('.current').removeClass('current').next('dd').hide();
var current = smartTabs.parent('dt').addClass('current').next('dd').show();
};
};
};
});
};
base.resize = function () {
base.viewport = $(window).outerWidth();
if (base.viewport <= base.options.breakpoint && base.options.layout == 'auto') {
base.$el.removeClass('tabs');
base.$el.addClass('accordion');
}
else if (base.viewport > base.options.breakpoint && base.options.layout == 'auto') {
base.$el.removeClass('accordion');
base.$el.addClass('tabs');
};
base.init();
};
// Run Functions
base.init();
base.navigation();
$(window).on('resize', function () {
base.resize();
});
};
$.cornerstoneUX.smartTabs.defaultOptions = {
activeClass: 'current',
breakpoint: 768,
breakTrigger: $(window),
contentHeight: 'fixed',
layout: 'auto'
};
$.fn.smartTabs = function(options){
return this.each(function(){
(new $.cornerstoneUX.smartTabs(this, options));
});
};
// This function breaks the chain, but returns
// the smartTabs if it has been attached to the object.
$.fn.getcornerstoneUX_smartTabs = function(){
this.data('cornerstoneUX.smartTabs');
};
})(jQuery);