实例: 自定义滚动条
<!DOCTYPE html>
<html lang="zh">
<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 type="text/css">
*{
margin: 0;
padding: 0;
}
#main{
width: 800px;
height: 400px;
margin: 50px auto;
border: 1px solid #ccc;
/*超出隐藏*/
overflow: hidden;
}
#left{
width: 760px;
height: 400px;
float: left;
}
#right{
width: 40px;
height: 400px;
float: right;
background-color: #ccc;
/*拖拽元素根据它进行定位*/
position: relative;
}
#move{
width: 40px;
height: 80px;
cursor: pointer;
border-radius: 20px;
background-color: #999;
box-shadow: 0 0 2px 1px #666;
/*定位钙元素*/
position: absolute;
left: 0;
top: 0;
}
/*文章div*/
#page{
position: relative;
top: 0px;
}
</style>
</head>
<body>
<div id="main">
<div id="left">
<div id="page">
真实的top值等于=鼠标到可视区的距离-鼠标到事件源的距离-大盒子到可视区的距离 .................
</div>
</div>
<div id="right">
<div id="move"></div>
</div>
</div>
<script type="text/javascript">
//需要改变的文章的高度
var h = page.offsetHeight - left.offsetHeight;
//大盒子面到可视区的距离
var clientY = main.getBoundingClientRect().top;
//右侧滚动条
move.onmousedown = function(ev){
var startY = ev.offsetY;//存储鼠标到事件源的距离
document.onmousemove = function(ev){
//真实的top值等于=鼠标到可视区的距离-鼠标到事件源的距离-大盒子到可视区的距离
var top_y = ev.clientY - startY - clientY;
if(top_y<0){
top_y = 0;
}else if(top_y>(right.offsetHeight-move.offsetHeight)){
top_y = right.offsetHeight-move.offsetHeight;
}
move.style.top = top_y + "px";
//计算比例
var scale = top_y/(right.offsetHeight-move.offsetHeight);
document.title = scale;
page.style.top = -h * scale + "px";
}
document.onmouseup = function(){
document.onmousemove = null;
document.onmouseup = null;
}
//清除默认行为
return false;
}
</script>
</body>
</html>
本文暂时没有评论,来添加一个吧(●'◡'●)