-
Notifications
You must be signed in to change notification settings - Fork 2
/
Tabs.html
96 lines (91 loc) · 2.4 KB
/
Tabs.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
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<script src="https://code.jquery.com/jquery-3.1.0.js"></script>
<meta name="viewport" content="width=device-width" />
<title>Tabs</title>
<style>
.tabs > ol {
list-style: none;
margin: 0;
padding: 0;
}
.tabs > ol.tabs-bar {
display: flex;
border-bottom: 1px solid red;
}
.tabs > ol.tabs-bar > li {
padding: 4px 8px;
border: 1px solid transparent;
border-bottom: 0;
}
.tabs > ol.tabs-bar > li:hover {
border-color: red;
}
.tabs > ol.tabs-bar > li.active {
border-color: blue;
}
.tabs > ol.tabs-content > li {
display: none;
}
.tabs > ol.tabs-content > li.active {
display: block;
}
</style>
</head>
<body>
<div class="tabs my-tabs">
<ol class="tabs-bar">
<li>1</li>
<li>2</li>
<li>3</li>
</ol>
<ol class="tabs-content">
<li>content 1</li>
<li>content 2</li>
<li>content 3</li>
</ol>
</div>
<div class="tabs xxx">
<ol class="tabs-bar">
<li>1</li>
<li>2</li>
<li>3</li>
</ol>
<ol class="tabs-content">
<li>content 1</li>
<li>content 2</li>
<li>content 3</li>
</ol>
</div>
</body>
<script>
class Tabs {
constructor(selector) {
this.elements = $(selector)
this.init()
this.bindEvents()
}
init() {
this.elements.each((index, element) => {
$(element).children('.tabs-bar').children('li').eq(0).addClass('active')
$(element).children('.tabs-content').children('li').eq(0).addClass('active')
})
}
bindEvents() {
this.elements.on('click', '.tabs-bar > li', e => {
const $li = $(e.currentTarget)
$li.addClass('active').siblings().removeClass('active')
const index = $li.index()
const $content = $li.closest('.tabs').find('.tabs-content>li').eq(index)
$content.addClass('active').siblings().removeClass('active')
})
}
}
// -----------------------------------------------------
// ----------------------- 使用 -------------------------
// -----------------------------------------------------
const tabs = new Tabs('.my-tabs')
</script>
</html>