forked from heuuLZP/css-layout
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mobile.html
83 lines (82 loc) · 1.92 KB
/
mobile.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
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width,initial-scale=1,user-scalable=no">
<title>移动端tab</title>
<style>
* {
margin: 0;
padding: 0;
}
ul, li {
list-style: none;
}
li {
float: left;
height: 30px;
line-height: 30px;
width: 30%;
text-align: center;
}
.active {
border-bottom: 1px solid pink;
}
.clearfix {
overflow: auto;
zoom: 1;
}
.tab-content {
height: 300px;
padding-left: 20px;
padding-top: 20px;
border: 1px solid #ececec;
}
</style>
</head>
<body>
<div class="wrapper">
<!-- tab-tittle -->
<ul class="clearfix">
<li data-index="0" class="active">标题一</li>
<li data-index="1">标题二</li>
<li data-index="2">标题三</li>
</ul>
<!-- tab-content -->
<div class="tab-content">
内容一
</div>
</div>
</body>
<script>
// 大写数字
var arr = ["一", "二", "三"];
/**
* 切换标题
*/
function clickTab(event) {
// 获取dom节点
var liArray = document.querySelectorAll('li');
var tabContent = document.querySelector('.tab-content')
var index;
if (event.target.tagName === 'LI') {
index = event.target.dataset.index;
// 处理内容区域
tabContent.innerHTML = '内容' + arr[index];
// 处理tab标题
for (var i = 0; i < liArray.length; i++) {
if(i == index) {
liArray[i].className = 'active'
} else {
liArray[i].className = ''
}
}
} else {
alert('异常错误!')
}
}
// 事件委托
var ul = document.querySelector('ul')
ul.addEventListener('click', clickTab, false)
</script>
</html>