/
tab.html
128 lines (121 loc) · 2.87 KB
/
tab.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
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>tab</title>
<style>
*{
list-style: none;
margin: 0;
padding: 0;
}
.wrap{
min-width: 480px;
text-align: center;
min-height: 10px;
}
.tab{
margin: 100px;
border: 1px solid #ccc;
background-color: #333;
color: #fff;
border-radius: 4px;
}
.tab>.tab-header>.active{
background-color: #fed136;
color: #000;
border-radius: 4px;
}
.tab>.tab-header>li{
float: left;
margin: 10px 10px 10px 10px;
cursor: pointer;
padding: 10px 20px;
border-radius: 4px;
}
.tab>.tab-header>li:hover{
color: #fed136;
}
.tab>.tab-header>.active:hover{
color: #fff;
}
.clearfix:after{
content: '';
display: block;
clear: both;
}
.tab>.tab-content>li{
display: none;
}
.tab>.tab-content>li.active{
margin: 0 10px 10px 10px;
padding: 40px 0;
display: block;
background-color: #fcfaf2;
color: #414141;
border-radius: 4px;
text-align: center;
}
</style>
</head>
<body>
<div class="wrap">
<div class="tab">
<ul class="tab-header clearfix">
<li class="active">tab1</li>
<li>tab2</li>
<li>tab3</li>
</ul>
<ul class="tab-content">
<li class="active">ct1</li>
<li>ct2</li>
<li>ct3</li>
</ul>
</div>
<div class="tab">
<ul class="tab-header clearfix">
<li class="active">tab1</li>
<li>tab2</li>
<li>tab3</li>
</ul>
<ul class="tab-content">
<li class="active">ct1</li>
<li>ct2</li>
<li>ct3</li>
</ul>
</div>
</div>
<script src="https://cdn.bootcss.com/jquery/3.2.1/jquery.min.js"></script>
<script>
//原生js面向对象实现
function Tab(ct) {
this.ct = ct;
this.init();
this.bind();
}
Tab.prototype.init = function(){
this.tabLis = this.ct.querySelectorAll('.tab-header>li');
this.tabPanels = this.ct.querySelectorAll('.tab-content>li');
};
Tab.prototype.bind = function(){
var that = this;
this.tabLis.forEach(function(tabli) {
tabli.onclick = function(e){
var target = e.target;
var index = [].indexOf.call(that.tabLis, target);
that.tabLis.forEach(function(li){
li.classList.remove('active');
});
target.classList.add('active');
that.tabPanels.forEach(function(panel){
panel.classList.remove('active');
});
that.tabPanels[index].classList.add('active');
}
});
};
var tab1 = new Tab(document.querySelectorAll('.tab')[0]);
var tab2 = new Tab(document.querySelectorAll('.tab')[1]);
</script>
</body>
</html>