大神帮我看看这段js代码,为什么一直抖动,我希望的效果是小方块能跟随鼠标移动
<!DOCTYPEhtml><htmllang="en"><head><metacharset="UTF-8"><title></title><style>#div1{h...
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title></title>
<style>
#div1{height: 200px;width: 150px;background: peru;position: absolute;right: 0;bottom: 0;}
#body1{height: 2000px}
</style>
<script>
window.onscroll=function()
{
var oDiv=document.getElementById('div1');
var oScrllTop=document.documentElement.scrollTop||document.body.scrollTop;
var timer=null;
//oDiv.style.top=(document.documentElement.clientHeight-oDiv.offsetHeight)/2+oScrllTop+'px';
startMove(parseInt((document.documentElement.clientHeight-oDiv.offsetHeight)/2)+oScrllTop);
function startMove(itarget){
clearInterval(timer);
timer=setInterval(function(){
var speed=(itarget-oDiv.offsetTop)/8;
speed=speed>0?Math.ceil(speed):Math.floor(speed);
if(oDiv.offsetTop==itarget){clearInterval(timer);}
else{
oDiv.style.top=oDiv.offsetTop+speed+'px';}
},30)
}
}
</script>
</head>
<body id="body1">
<div id="div1"></div>
</body>
</html> 展开
<html lang="en">
<head>
<meta charset="UTF-8">
<title></title>
<style>
#div1{height: 200px;width: 150px;background: peru;position: absolute;right: 0;bottom: 0;}
#body1{height: 2000px}
</style>
<script>
window.onscroll=function()
{
var oDiv=document.getElementById('div1');
var oScrllTop=document.documentElement.scrollTop||document.body.scrollTop;
var timer=null;
//oDiv.style.top=(document.documentElement.clientHeight-oDiv.offsetHeight)/2+oScrllTop+'px';
startMove(parseInt((document.documentElement.clientHeight-oDiv.offsetHeight)/2)+oScrllTop);
function startMove(itarget){
clearInterval(timer);
timer=setInterval(function(){
var speed=(itarget-oDiv.offsetTop)/8;
speed=speed>0?Math.ceil(speed):Math.floor(speed);
if(oDiv.offsetTop==itarget){clearInterval(timer);}
else{
oDiv.style.top=oDiv.offsetTop+speed+'px';}
},30)
}
}
</script>
</head>
<body id="body1">
<div id="div1"></div>
</body>
</html> 展开
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询