#mask2{
width: 10px;
background-color: black;
border-radius: 5px;
cursor: pointer;
}
//跟随屏幕移动栏
var follow1 = $('follow_1');
var follow1Left = follow1.children[0];
var follow1Right = follow1.children[1];
var mask2 = follow1Right.children[0];
//设置滚动条长度
//滚动条长度=盒子长度/内容长度*盒子长度
mask2.style.height = follow1.offsetHeight / follow1Left.offsetHeight * follow1.offsetHeight + 'px';
mask2.onmousedown=function (event) {
//获取初始位置
var offsetTop = event.clientY - mask2.offsetTop;
//移动
mask2.onmousemove=function (event) {
var y = event.clientY - offsetTop;
//边界值处理
if(y<0){
y = 0;
}else if (y>=follow1Right.offsetHeight - mask2.offsetHeight){
y = follow1Right.offsetHeight - mask2.offsetHeight;
}
mask2.style.top = y + 'px';
//内容走的距离 = (内容长度-盒子长度)/(盒子长度-滚动条长度)* 滚动条距离
follow1Left.style.top = -((follow1Left.offsetHeight-follow1.offsetHeight)/(follow1.offsetHeight-mask2.offsetHeight) * y )+'px';
return false;
};
mask2.onmouseup= function () {
mask2.onmousemove = null;
}
};
follow1Right.onmousedown=function (event) {
//获取初始位置
var offsetTop = event.clientY - mask2.offsetTop;
//移动
var y = event.clientY - follow1.offsetTop - mask2.offsetHeight*0.5;
//边界值处理
if(y<0){
y = 0;
}else if (y>=follow1Right.offsetHeight - mask2.offsetHeight){
y = follow1Right.offsetHeight - mask2.offsetHeight;
}
mask2.style.top = y + 'px';
//内容走的距离 = (内容长度-盒子长度)/(盒子长度-滚动条长度)* 滚动条距离
follow1Left.style.top = -((follow1Left.offsetHeight-follow1.offsetHeight)/(follow1.offsetHeight-mask2.offsetHeight) * y )+'px';
};
想让元素移动,必须加上定位!!!!!!!!!!!
#mask2{
width: 10px;
background-color: black;
border-radius: 5px;
cursor: pointer;
position: absolute;
}