-
Notifications
You must be signed in to change notification settings - Fork 1
/
tabs.js
43 lines (38 loc) · 1.43 KB
/
tabs.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
(function($){
$.fn.tabs = function ( options ) {
return this.each( function () {
var menu,
mkp = '',
$this = $(this),
tmpl = '<li class="{class}"><a href="#{id}"><span>{title}</span></a></li>',
elements = $this.find('.tab-container');
//Return if no tab containers found
if( !$this.find('.tab-container').length ){
return;
}
// Overwrite defaults
defaults = {
menu: null,
show: 0, //index of tab to show on init
onShow: function(){}
};
options = $.extend( {}, defaults, options );
// Create markup
for( i=0; i<elements.length; i++ ){
cls = i === 0 ? 'lefttab' : ( i === elements.length - 1 ? 'righttab' : '' );
mkp += tmpl.replace( '{class}', cls )
.replace( '{id}', $(elements[i]).attr('id') )
.replace( '{title}', $(elements[i]).attr('title') );
}
mkp = '<ul>' + mkp + '</ul>';
menu = options.menu ? $( mkp ).prependTo( options.menu ) : $( '<div class="tabs-menu">'+mkp+'</div>' ).prependTo( $this );
// Bind the keyup event, and show the desired tab
menu.find('li').on( 'click.tabs', function ( event ) {
$(this).addClass('tabactive').siblings().removeClass('tabactive');
$this.find( '#' + $(this).find('a').attr('href') ).show().siblings('.tab-container').hide();
options.onShow.call( $(this) );
event.preventDefault();
}).eq( options.show ).trigger( 'click.tabs' );
});
};
})(jQuery);