/
20tabs.html
187 lines (146 loc) · 4.33 KB
/
20tabs.html
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
<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<style type="text/css">
#div1 div,#div2 div,#div3 div,#div4 div {width:200px;height:200px;border:1px #000 solid;display: none;}
.active {background: red;}
</style>
<script src="js/jquery-1.11.0.min.js"></script>
<script>
/**
title : 基于JQ的选项卡组件
options : 配置参数
event : 触发事件,默认click
delay : 切换选项卡的延迟,默认 0
Methods : 方法 // 方法和配置参数 没有太严格的区分,其实这里的 nowSel也可以弄成配置参数,而不是事件,没有严格解析,看自己习惯
nowSel() : 设置默认选项卡起始位置(即第几个选项卡显示
getContent() : 获取当前选项卡内容、
Events:自定义事件
beforeClick : 点击前触发
afterClick : 点击后触发
*/
//JQ中的主动触发方法: trigger()
$(function(){
var t1 = new Tab();
t1.init('div1',{});
var t2 = new Tab();
t2.init('div2',{
event:'mouseover'
});
var t3 = new Tab();
t3.init('div3',{
event:'mouseover',
delay:200
});
var t4 = new Tab();
t4.init('div4',{});
t4.nowSel(2); // 初始是第3个选项卡显示
// 点击inut1,弹出当前选项卡内容
$("#input1").click(function(){
t4.getContent();
});
// 在jquery中,t4是对象,不能直接加on,要用 $ 包裹一下才能加on
$(t4).on('beforeClick',function(){
// 点击前弹一下当前选项卡内容
alert(t4.getContent());
});
$(t4).on('afterClick',function(){
// 点击后弹一下当前选项卡内容
alert(t4.getContent());
});
});
function Tab(){
this.oParent = null;
this.aInput = null;
this.aDiv = null;
this.iNow = 0;
this.settings = { // 默认参数
event : 'click',
delay:0
}
}
Tab.prototype.init = function(oParent,opt){
$.extend( this.settings,opt );
this.oParent = $('#'+oParent);
this.aInput = this.oParent.find('input');
this.aDiv = this.oParent.find('div');
this.change();
};
Tab.prototype.change = function(){
var This = this;
var timer = null;
this.aInput.on(this.settings.event,function(){
var _this = this;
if(This.settings.event == 'mouseover' && This.settings.delay ){
timer = setTimeout(function(){
show(_this);
},This.settings.delay)
}else {
show(this);
}
}).mouseout(function(){
clearTimeout(timer);
});
function show(obj){
$(This).trigger('beforeClick'); // 主动触发事件
This.aInput.attr('class','');
This.aDiv.css("display",'none');
$(obj).attr('class','active');
This.aDiv.eq( $(obj).index() ).css("display",'block');
This.iNow = $(this).index();
$(This).trigger('afterClick'); // 主动触发事件
}
};
Tab.prototype.nowSel = function(index){
this.aInput.attr('class','');
this.aDiv.css("display",'none');
this.aInput.eq(index).attr('class','active');
this.aDiv.eq(index ).css("display",'block');
this.iNow = index;
}
// 获取当前选项卡内容
Tab.prototype.getContent = function(){
return this.aDiv.eq(this.iNow).html();
}
</script>
</head>
<body>
<div id="div1">
<input class="active" type="button" value="1">
<input type="button" value="2">
<input type="button" value="3">
<div style="display: block;">1111</div>
<div>222</div>
<div>333</div>
</div>
<div id="div2">
<input class="active" type="button" value="1">
<input type="button" value="2">
<input type="button" value="3">
<div style="display: block;">1111</div>
<div>222</div>
<div>333</div>
</div>
<div id="div3">
<input class="active" type="button" value="1">
<input type="button" value="2">
<input type="button" value="3">
<div style="display: block;">1111</div>
<div>222</div>
<div>333</div>
</div>
<div id="div4">
<input class="active" type="button" value="1">
<input type="button" value="2">
<input type="button" value="3">
<div style="display: block;">1111</div>
<div>222</div>
<div>333</div>
</div>
<input type="button" value="点击" id="input1">
</body>
</html>