-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathScrollbar.html
88 lines (82 loc) · 2.51 KB
/
Scrollbar.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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Scrollbar</title>
<style>
*{
padding: 0;
margin: 0;
}
#container{
float: left;
height: 200px;
overflow: hidden;
position: relative;
}
#content{
width: 200px;
background-color: #cccccc;
float: left;
position: relative;
}
#scroll-bar{
width: 20px;
height: 200px;
background-color: #999999;
float: left;
}
#block{
height: 20px;
background-color: red;
position: relative;
}
</style>
</head>
<body>
<div id="container">
<div id="content">
啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊
</div>
<div id="scroll-bar">
<div id="block"></div>
</div>
</div>
<script>
(function () {
var oContainer = document.getElementById("container");
var oContent = document.getElementById("content");
var oBlock = document.getElementById("block");
var oScroll = document.getElementById("scroll-bar");
console.log(oContent.offsetHeight);
oBlock.onmousedown = function (e) {
e = e|| window.event;
var iDisY = e.clientY - this.offsetTop;
document.onmousemove = function (e) {
e = e || window.event;
var top = e.clientY - iDisY;
var iCmax = oContent.offsetHeight-oContainer.offsetHeight;
console.log(iCmax);
var iMax = oScroll.offsetHeight - oBlock.offsetHeight;
var rate = (oContent.offsetHeight-oContainer.offsetHeight)/iMax;
var iMin = 0;
oContent.style.top = -rate*top + 'px';
oBlock.style.top = top + 'px';
if(oBlock.offsetTop >= iMax){
oBlock.style.top = iMax + 'px';
oContent.style.top = -iCmax + 'px';
}
if(oBlock.offsetTop <= iMin){
oBlock.style.top = iMin + 'px';
oContent.style.top = iMin + 'px';
}
return false;
}
};
document.onmouseup = function () {
document.onmousemove = null;
}
})();
</script>
</body>
</html>