-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtabTest.html
More file actions
170 lines (154 loc) · 4.66 KB
/
tabTest.html
File metadata and controls
170 lines (154 loc) · 4.66 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
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style type="text/css">
*{
margin: 0;
padding: 0;
}
html,body{
width: 100%;
height: 100%;
background: yellow;
}
div.tab{
width: 100%;
height: 50%;
background: white;
}
div.tablist{
width: 15%;
height: 100%;
border: solid red;
display: inline-block;
vertical-align: top;
stroke-dasharray:
}
ul{
/*background: green;*/
}
div.tabcontent{
width: 83%;
height: 100%;
border: solid green;
display: inline-block;
/*background: green;*/
vertical-align: top;
}
.selected{
background: #f0f0f0;
}
.unselected{
background: #fff0f0;
}
</style>
</head>
<body>
<div id="tab1" class="tab">
<div class="tablist">
<ul class="tabli">
<li>tab1</li>
<li>tab2</li>
<li>tab3</li>
</ul>
<button type="button">指定选项卡3</button>
</div>
<div class="tabcontent">
<div>conten1</div>
<div>conten2</div>
<div>conten3</div>
</div>
</div>
<div id="tab2" class="tab">
<div class ="tablist tabli">
<div>tab1</div>
<div>tab2</div>
<div>tab3</div>
</div>
<div class="tabcontent">
<div>content1</div>
<div>content2</div>
<div>content3</div>
</div>
</div>
<script type="text/javascript">
var options = {
dom: '#tab2 .tablist div',
content: '#tab2 .tabcontent > div',
defaultSelected: 1,
event: 'mouseover',
selected: 'selected',
callback: function(tabIndex){
alert(tabIndex + ' selected');
}
};
const DEFAULTS = {
defaultSelected: 0,
event: 'click'
}
var tab = new Tab(options);
document.querySelectorAll('#tab1 .tablist button')[0].addEventListener('click',function(){
tab.showContent(2);
options.callback(2);
})
function Tab(options){
options = Object.assign({},DEFAULTS,options);
console.log(options);
var tabList = document.querySelectorAll(options.dom);
var contentList = document.querySelectorAll(options.content);
var timer = '';
var lastTabIndex = 0;
var _this = this;
//为Tab绑定索引
for(let i = 0; i < tabList.length; i++){
tabList[i].index = i;
}
//清除所有内容
for(let content of contentList){
content.style.display = 'none';
}
console.log('events',tabList[1].index,contentList);
this.init = function(){
this.showContent(options.defaultSelected);
checkEvent(options.event);
}
function checkEvent(e){
if(e == 'click'){
handleClick();
}else{
handleMouseover();
}
}
function handleClick(){
tabList[0].parentNode.addEventListener('click',function(e){
var tar = e.target;
_this.showContent(tar.index);
options.callback(tar.index);
})
}
function handleMouseover(){
tabList[0].parentNode.addEventListener('mouseover',function(e){
timer = setTimeout(function(){
var tar = e.target;
_this.showContent(tar.index);
options.callback(tar.index);
},2000)
});
tabList[0].parentNode.addEventListener('mouseout',function(){
clearTimeout(timer);
})
}
this.showContent = function(tabIndex){
tabList[lastTabIndex].removeAttribute('class');
tabList[tabIndex].setAttribute('class',options.selected);
contentList[lastTabIndex].style.display = 'none';
contentList[tabIndex].style.display = 'block';
lastTabIndex = tabIndex;
}
this.init();
}
</script>
</body>
</html>