-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtab.js
89 lines (77 loc) · 1.96 KB
/
tab.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
;(function($){
var Tab = function(tab){
var _this_ = this;
this.tab = tab;
this.tabItems = this.tab.find("ul.tab-nav li");
this.contentItems = this.tab.find("div.content-wrap div.content-item");
this.loop = 0;
this.config = {
"triggerType":"mouseover",
"effect":"defult",
"invoke":"2",
"auto":5000
}
if(this.getConfig()){
$.extend(this.config,this.getConfig())
}
console.log(this.config)
var config = this.config;
if(config.triggerType === "click"){
this.tabItems.bind(config.triggerType,function(){
_this_.invoke($(this));
_this_.loop = $(this).index();
});
}else if(config.triggerType === "mouseover"||config.triggerType !== "click"){
this.tabItems.bind("mouseover",function(){
_this_.invoke($(this));
});
};
if(config.auto){
this.timer = null;
this.autoPlay()
this.tab.hover(function(){
clearInterval(_this_.timer)
},function(){
_this_.autoPlay()
})
}
}
Tab.prototype = {
//获取配置参数
getConfig:function(){
var config = this.tab.attr("data-config")
if(config&&config!==""){
return $.parseJSON(config)
}else{
return null;
}
},
//事件驱动函数
invoke:function(currentTab){
var _this_ = this;
var index = currentTab.index();
var effect = this.config.effect;
var contentItem = this.contentItems;
currentTab.addClass("active").siblings().removeClass();
if(effect === "default"||effect !== "fade"){
contentItem.eq(index).addClass("current").siblings().removeClass("current")
}else if(effect === "fade"){
contentItem.eq(index).fadeIn().siblings().fadeOut();
}
},
autoPlay:function(){
var self = this,
tabItems = this.tabItems,
tabLength = tabItems.size(),
config = this.config;
this.timer = window.setInterval(function(){
self.loop++;
if(self.loop>=tabLength){
self.loop = 0;
}
tabItems.eq(self.loop).trigger(config.triggerType)
},config.auto)
}
}
window.Tab = Tab
})(jQuery)