求问:有时候当鼠标移入他不会停下来,会一直往前走,突然又跑回来了;有一两次正正常的。
<DOCTYPE! html>
<head>
<meta charset="utf-8">
<title>动起来</title>
<link rel="stylesheet" type="text/css" href="style.css">
<script type="text/javascript">
window.onload = function(){
var oTotal = document.getElementById('total');
oTotal.onmouseover = function(){
startMove(0);
}
oTotal.onmouseout = function(){
startMove(-150);
}
}
var timer=null;
function startMove(iTarget){
clearInterval(timer);
var oTotal = document.getElementById('total');
var speed = (iTarget-oTotal.offsetLeft)/15;
speed=speed>0?Math.ceil(speed):Math.floor(speed);
timer = setInterval(function(){
if(oTotal.offsetLeft == iTarget){
clearInterval(timer);
}else{
oTotal.style.left=oTotal.offsetLeft+speed+'px';
}
},30);
}
</script>
</head>
<body>
<div id="total">
<div id="share">
</div>
<span>分享</span>
</body>
</html>