javascript 滾動(dòng)條
來(lái)源:程序員人生 發(fā)布時(shí)間:2014-12-22 08:35:24 閱讀次數(shù):3895次
轉(zhuǎn)動(dòng)條的實(shí)現(xiàn)原理和上1篇文章中的拖拽有很大關(guān)系,滑動(dòng)條就是通過(guò)拖拽實(shí)現(xiàn)的,通過(guò)計(jì)算滑動(dòng)條的拖拽區(qū)間來(lái)得出1個(gè)比例scale,這個(gè)就是我們要用到的文字轉(zhuǎn)動(dòng)距離了,
div3里別忘記添加文字
具體代碼以下
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf⑻" />
<title>New Web Project</title>
<style>
#parent{
width:600px;
height:20px;
background:#CCCCCC;
position: relative;
}
#div1{
width: 20px;
height:20px;
top:0px;
left:0px;
background:#FF0000;
position: absolute;
}
#div2{
width:300px;
height:300px;
border:1px solid #222222;
position:relative;
margin-top:10px;
overflow:hidden;
}
#div3{
padding:5px;
position: absolute;
}
</style>
<script>
window.onload=function(){
var oParent=document.getElementById('parent');
var oDiv1=document.getElementById('div1');
var oDiv2=document.getElementById('div2');
var oDiv3=document.getElementById('div3');
oDiv1.onmousedown=function(evt){
var e=evt||event;
var disX=e.clientX-oDiv1.offsetLeft;
document.onmousemove=function(evt1){
var e1=evt1||event;
var posX=e1.clientX-disX;
if(posX<0)
{
posX=0;
}
else if(posX>oParent.offsetWidth-oDiv1.offsetWidth)
{
posX=oParent.offsetWidth-oDiv1.offsetWidth;
}
oDiv1.style.left=posX+'px';
var scale=posX/(oParent.offsetWidth-oDiv1.offsetWidth);
oDiv3.style.top=-(oDiv3.offsetHeight-oDiv2.offsetHeight)*scale+'px';
};
document.onmouseup=function(){
document.onmousemove=null;
document.onmouseup=null;
};
return false;
};
};
</script>
</head>
<body>
<div id="parent">
<div id="div1"></div>
</div>
<div id="div2">
<div id="div3">
</div>
</div>
</body>
</html>
運(yùn)行結(jié)果圖:

生活不易,碼農(nóng)辛苦
如果您覺(jué)得本網(wǎng)站對(duì)您的學(xué)習(xí)有所幫助,可以手機(jī)掃描二維碼進(jìn)行捐贈(zèng)