-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathjquery.scrolltab.js
160 lines (136 loc) · 5.9 KB
/
jquery.scrolltab.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
/*!
* jQuery UI Tab Scroll Plugin V 1.0
* http://riteshsblog.blogspot.com/
*
* Copyright 2011, Ritesh Jha (mailrkj at gmail.com)
* Code Licensed Apache License 2.0
* Date: Thu Sep 1 2011
*/
(function($){
$.fn.scrolltab = function(options) {
var settings = {
ulid: 'tabUl',
leftId:'left',
rightId:'right',
visibleTab:0,
totalTab:0
};
return this.each(function(){
var options = $.extend(settings, options);
options.tabWidth = 0;
options.maxLeft = 0;
options.totalTab = 0;
$tabs = $(this),
$tabUL = $('#'+options.ulid);
$left = $('#'+options.leftId);
$right = $('#'+options.rightId);
$tabUL.css('width',$tabs.width());
/*tab slider*/
$left.click(function() {
if($tabUL.position().left > (options.maxLeft)){
$left.hide();
$tabUL.animate({
left: '-='+options.tabWidth
},300, function(){
$tabs.trigger('showArrow');
});
}
});
$right.click(function() {
if($tabUL.position().left < 0){
$right.hide();
$tabUL.animate({
left: '+='+options.tabWidth
} ,300, function(){
$tabs.trigger('showArrow');
});
}
});
//if tab closed
$tabs.find("span.ui-icon-close" ).live( "click", function() {
var index = $( "li", $tabs ).index( $( this ).parent() );
$tabs.tabs( "remove", index );
//update totalTab
options.totalTab = $('li', $tabUL).length;
$tabs.trigger('tabMaxLeft');
if($tabUL.position().left < 0){
$tabUL.css('width', ($tabUL.width() - options.tabWidth) + 'px');
$right.trigger('click');
}else if($tabUL.position().left > (options.maxLeft)){
$tabUL.css('width', ($tabUL.width() - options.tabWidth) + 'px');
$left.trigger('click');
}
if(options.totalTab <= 0){
$tabs.css('visibility','hidden');
}
});
//bind event
$tabs.bind('addTab',function(event,id,title){
var tabid = '#tabs-'+id;
$tabs.tabs('add', tabid, title);
//increase totalTab
options.totalTab = $('li', $tabUL).length;
//add width in ul
if(options.visibleTab == 0){ //calculate number of visible tab
options.tabWidth = $("li", $tabUL).outerWidth(true);
options.visibleTab = Math.floor($tabs.width()/options.tabWidth);
}
//add width in tab ul
if(options.totalTab > options.visibleTab){
var cuiwidth = $tabUL.width();
$tabUL.css('width', (cuiwidth + options.tabWidth) + 'px');
$tabs.trigger('tabMaxLeft');
$tabs.trigger('selectTab',[options.totalTab - 1 ]);
}else{
$tabs.tabs('select', (options.totalTab - 1));
}
});
$tabs.bind('tabMaxLeft', function(){
var extraTabs = options.totalTab - options.visibleTab;
options.maxLeft = -(extraTabs * options.tabWidth);
});
$tabs.bind('selectTab', function(event, index){
$tabs.tabs('select', index);
index++;
var currentleftPos = $tabUL.position().left;
var tabHiddenLeft = Math.round(Math.abs(currentleftPos)/options.tabWidth);
if(tabHiddenLeft >= index){
var rigtAnimate = (tabHiddenLeft - index +1 ) * options.tabWidth;
$tabUL.animate({
left: '+='+rigtAnimate
},300, function() {
$tabs.trigger('showArrow');
});
}else{
var tabonleft = tabHiddenLeft + options.visibleTab;
var tabHiddenRight = options.totalTab - tabHiddenLeft - options.visibleTab;
if(tabHiddenRight > 0 && index > tabonleft){
var rightIndex = (index - tabonleft);
var leftAnimate = rightIndex * options.tabWidth;
$tabUL.animate({
left: '-='+leftAnimate
},300, function(){
$tabs.trigger('showArrow');
});
}
}
});
$tabs.bind('showArrow', function(){
var currentleftPos = $tabUL.position().left;
var tabHiddenLeft = Math.round(Math.abs(currentleftPos)/options.tabWidth);
var tabonleft = tabHiddenLeft + options.visibleTab;
var tabHiddenRight = options.totalTab - tabHiddenLeft - options.visibleTab;
if(tabHiddenLeft > 0){
$right.show();
}else{
$right.hide();
}
if(tabHiddenRight > 0){
$left.show();
}else{
$left.hide();
}
});
});// return part
}; //function body end
})(jQuery);