-
Notifications
You must be signed in to change notification settings - Fork 110
/
jquery.tabslet.js
246 lines (162 loc) · 5.75 KB
/
jquery.tabslet.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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
/**
* Tabslet | tabs jQuery plugin
*
* @copyright Copyright 2015, Dimitris Krestos
* @license Apache License, Version 2.0 (http://www.opensource.org/licenses/apache2.0.php)
* @link http://vdw.staytuned.gr
* @version v1.7.3
*/
/* Sample html structure
<div class='tabs'>
<ul class='horizontal'>
<li><a href="#tab-1">Tab 1</a></li>
<li><a href="#tab-2">Tab 2</a></li>
<li><a href="#tab-3">Tab 3</a></li>
</ul>
<div id='tab-1'></div>
<div id='tab-2'></div>
<div id='tab-3'></div>
</div>
OR
<div class='tabs'>
<ul class='horizontal'>
<li><a href="#tab-1">Tab 1</a></li>
<li><a href="#tab-2">Tab 2</a></li>
<li><a href="#tab-3">Tab 3</a></li>
</ul>
</div>
<div id='tabs_container'>
<div id='tab-1'></div>
<div id='tab-2'></div>
<div id='tab-3'></div>
</div>
*/
;(function($, window, undefined) {
"use strict";
$.fn.tabslet = function(options) {
var defaults = {
mouseevent: 'click',
activeclass: 'active',
attribute: 'href',
animation: false,
autorotate: false,
deeplinking: false,
pauseonhover: true,
delay: 2000,
active: 1,
container: false,
controls: {
prev: '.prev',
next: '.next'
}
};
var options = $.extend(defaults, options);
return this.each(function() {
var $this = $(this), _cache_li = [], _cache_div = [];
var _container = options.container ? $(options.container) : $this;
var _tabs = _container.find('> div');
// Caching
_tabs.each(function() { _cache_div.push($(this).css('display')); });
// Autorotate
var elements = $this.find('> ul > li'), i = options.active - 1; // ungly
if ( !$this.data( 'tabslet-init' ) ) {
$this.data( 'tabslet-init', true );
$this.opts = [];
$.map( ['mouseevent', 'activeclass', 'attribute', 'animation', 'autorotate', 'deeplinking', 'pauseonhover', 'delay', 'container'], function( val, i ) {
$this.opts[val] = $this.data(val) || options[val];
});
$this.opts['active'] = $this.opts.deeplinking ? deep_link() : ( $this.data('active') || options.active )
_tabs.hide();
if ( $this.opts.active ) {
_tabs.eq($this.opts.active - 1).show();
elements.eq($this.opts.active - 1).addClass(options.activeclass);
}
var fn = eval(
function(e, tab) {
var _this = tab ? elements.find('a[' + $this.opts.attribute + '="' + tab +'"]').parent() : $(this);
_this.trigger('_before');
elements.removeClass(options.activeclass);
_this.addClass(options.activeclass);
_tabs.hide();
i = elements.index(_this);
var currentTab = tab || _this.find('a').attr($this.opts.attribute);
if ($this.opts.deeplinking) location.hash = currentTab;
if ($this.opts.animation) {
_container.find(currentTab).animate( { opacity: 'show' }, 'slow', function() {
_this.trigger('_after');
});
} else {
_container.find(currentTab).show();
_this.trigger('_after');
}
return false;
}
);
var init = eval("elements." + $this.opts.mouseevent + "(fn)");
init;
var t;
var forward = function() {
i = ++i % elements.length; // wrap around
$this.opts.mouseevent == 'hover' ? elements.eq(i).trigger('mouseover') : elements.eq(i).click();
if ($this.opts.autorotate) {
clearTimeout(t);
t = setTimeout(forward, $this.opts.delay);
$this.mouseover(function () {
if ($this.opts.pauseonhover) clearTimeout(t);
});
}
}
if ($this.opts.autorotate) {
t = setTimeout(forward, $this.opts.delay);
$this.hover(function() {
if ($this.opts.pauseonhover) clearTimeout(t);
}, function() {
t = setTimeout(forward, $this.opts.delay);
});
if ($this.opts.pauseonhover) $this.on( "mouseleave", function() { clearTimeout(t); t = setTimeout(forward, $this.opts.delay); });
}
function deep_link() {
var ids = [];
elements.find('a').each(function() { ids.push($(this).attr($this.opts.attribute)); });
var index = $.inArray(location.hash, ids)
if (index > -1) {
return index + 1
} else {
return ($this.data('active') || options.active)
}
}
var move = function(direction) {
if (direction == 'forward') i = ++i % elements.length; // wrap around
if (direction == 'backward') i = --i % elements.length; // wrap around
elements.eq(i).click();
}
$this.find(options.controls.next).click(function() {
move('forward');
});
$this.find(options.controls.prev).click(function() {
move('backward');
});
$this.on ('show', function(e, tab) {
fn(e, tab);
});
$this.on ('next', function() {
move('forward');
});
$this.on ('prev', function() {
move('backward');
});
$this.on ('destroy', function() {
$(this)
.removeData()
.find('> ul li').each( function(i) {
$(this).removeClass(options.activeclass);
});
_tabs.each( function(i) {
$(this).removeAttr('style').css( 'display', _cache_div[i] );
});
});
}
});
};
$(document).ready(function () { $('[data-toggle="tabslet"]').tabslet(); });
})(jQuery);