-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscroll.html
217 lines (195 loc) · 6.29 KB
/
scroll.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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
<!DOCTYPE html>
<html lang="en">
<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>
body {
margin: 0;
padding: 0;
}
.container {
width: 100%;
height: 100%;
position: fixed;
}
.wrapper {
position: absolute;
left: 0;
top: 0;
width: 10%;
height: 100%;
background: #fff;
border-right: 1px solid #ccc;
}
.box1 {
position: absolute;
right: 0;
top: 0;
width: 90%;
height: 100%;
background: #eee;
overflow-y: auto;
}
.section {
font-size: 15px;
color: #333;
width: 100%;
height: 60px;
line-height: 60px;
text-align: center;
cursor: pointer;
background: #fff;
/* border-right: 1px solid #ccc; */
transition: all .5s;
}
.section:hover {
color: #04be02;
background: #eee;
}
.list {
width: 100%;
height: 500px;
}
.tit {
margin: 5px 0;
padding: 0;
width: 100%;
height: 50px;
line-height: 50px;
padding-left: 10px;
font-size: 15px;
background: #feefdf;
color: #333;
}
.active {
color: #04be02;
background: #eee;
/* border-right:1px solid #eee; */
}
</style>
</head>
<body>
<div class="container">
<div class="wrapper">
<div class="section">分类1</div>
<div class="section">分类2</div>
<div class="section">分类3</div>
<div class="section">分类4</div>
</div>
<div class="box1">
<div class="list">
<p class="tit">分类1</p>
<div class="list_cont">
内容
</div>
</div>
<div class="list">
<p class="tit">分类2</p>
<div class="list_cont">
内容
</div>
</div>
<div class="list">
<p class="tit">分类3</p>
<div class="list_cont">
内容
</div>
</div>
<div class="list">
<p class="tit">分类4</p>
<div class="list_cont">
内容
</div>
</div>
</div>
</div>
</body>
<script>
let $ = (obj) => {
if (typeof (obj) != 'number') {
if (obj.indexOf('#') != -1) {
return document.querySelector(obj);
} else if (obj.indexOf('.') != -1) {
let dom = document.querySelectorAll(obj);
let len = dom.length;
if (len === 1 && len) {
return dom[0];
} else {
return dom;
};
} else {
console.log(new Error('请检查是否存在该id或class'));
return undefined;
};
} else {
console.log(new Error('请传入id或class'));
return undefined;
};
};
scrolls(['.box1', '.section', '.list', 200, 'section', 'active', 2]);
function scrolls(obj) {
$(obj[0]).onscroll = function () {
var t = this.scrollTop; //获取滚动条距离顶部的位置
var len = $(obj[2]).length; //获取nav个数
for (var i = 0; i < len; i++) {
if (t >= $(obj[2])[i].offsetTop - obj[3]) {
// 判断顶部距离大于等于当前div的上边距
if (t < (to_scroll(i, true)) + obj[3] || t > (to_scroll(i, true)) + obj[3]) {
// 判断顶部距离小于div的高度乘于当前div的下标
for (var k = 0; k < len; k++) {
$(obj[1])[k].className = obj[4];
// 恢复初始值
};
$(obj[1])[i].classList.add(obj[5]);
// 添加样式
};
};
};
}; //滚动实时监听滚动条位置
(function () {
var len = $(obj[1]).length;
for (var i = 0; i < len; i++) {
$(obj[1])[i].className = obj[4];
nav_tab(i);
};
function nav_tab(i) {
$(obj[1])[i].addEventListener('click', function () {
to_scroll(i);
for (var j = 0; j < len; j++) {
$(obj[1])[j].className = obj[4]; // 恢复初始值
};
this.classList.add(obj[5]); // 添加样式
}, false);
};
// 点击侧边改变滚动条位置
})();
(function () {
var index = obj[6] || 0;
if (index) {
if (!(index - 1)) {
$(obj[0]).scrollTop = 1;
// 避免设置第一个默认显示的时候不会自动选中
} else {
to_scroll(index - 1);
};
} else {
$(obj[0]).scrollTop = 1;
};
})(); // 初始显示
function to_scroll(i, flag) {
var num = 0;
for (var l = 0; l < i; l++) {
num += $(obj[2])[l].offsetHeight;
};
if (!flag) {
$(obj[0]).scrollTop = num; //设置滚动条
} else {
return num; //返回滚动条的位置
};
}; //设置滚动条位置(滚动条的位置是obj[2]的下标(第几个)之间的总和)
};
</script>
</html>