aui-tab.js
2.24 KB
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
/**
* aui-tab.js
* @author 流浪男
* @todo more things to abstract, e.g. Loading css etc.
* Licensed under the MIT license.
* http://www.opensource.org/licenses/mit-license.php
*/
(function( window, undefined ) {
"use strict";
var auiTab = function(params,callback) {
this.extend(this.params, params);
this._init(callback);
};
var tabItems;
auiTab.prototype = {
params: {
element: false,
index: 1, //默认选中
repeatClick: false //是否允许重复点击
},
_init: function(callback) {
var self = this;
if(!self.params.element || self.params.element.nodeType!=1){
return;
}
tabItems = self.params.element.children;
if(tabItems){
self.setActive();
for(var i=0; i<tabItems.length; i++){
tabItems[i].setAttribute("tapmode","");
tabItems[i].setAttribute("data-item-order",i);
tabItems[i].onclick = function(e){
if(!self.params.repeatClick){
if(this.className.indexOf("aui-active") > -1)return;
}
if(callback){
callback({
index: parseInt(this.getAttribute("data-item-order"))+1,
dom:this
})
};
this.parentNode.querySelector(".aui-active").classList.remove("aui-active");
this.classList.add("aui-active");
}
}
}
},
setRepeat:function(value){
var self = this;
self.params.repeatClick = value ? value : false;
},
setActive: function(index){
var self = this;
index = index ? index : self.params.index;
var _tab = tabItems[index-1];
if(_tab.parentNode.querySelector(".aui-active"))_tab.parentNode.querySelector(".aui-active").classList.remove("aui-active");
_tab.classList.add("aui-active");
},
extend: function(a, b) {
for (var key in b) {
if (b.hasOwnProperty(key)) {
a[key] = b[key];
}
}
return a;
}
};
window.auiTab = auiTab;
})(window);