a.scrollTop的计算;
b.滚动元素的定位值计算;
c.滚动周期设定;
代码如下:
css部分:
/*测试用的高度*/
body{ height:3000px;}
div,ul,li,body{margin:0; padding:0;}
/*position:absolute;用于元素的定位*/
#roll{width:50px; height:100px; background:#99CC00; position:absolute;}
Html代码:
JS代码:
var roll=document.getElementById('roll'),
initX=0,
initY,
compY,
sp=15,
//可调整时间间隔,步进值不宜过大,不然IE下有点闪屏;
timeGap=5,
doc=document.documentElement,
docBody=document.body;
compY=initY=200;
roll.style.right=initX+"px";
;(function(){
var curScrollTop=(doc.scrollTop||docBody.scrollTop||0)-(doc.clientTop||docBody.clientTop||0);
//每次comP的值都不一样;直到roll.style.top===doc.scrollTop+initY;
compY+=(curScrollTop+initY-compY)/sp;
roll.style.top=Math.ceil(compY)+"px";
setTimeout(arguments.callee,timeGap);
})();