这是什么问题
var speed = (iTarget-obox.offsetLeft)/20;除以20的情况下反复用鼠标移动(等box全部出来鼠标再移开) 8、9次就开始出现bug了(移上移除算一次)。特别的如果/30立马就出现问题了?
<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<title>缓冲运动</title>
<style type="text/css">
*{margin: 0;padding: 0;}
#box{width: 200px;height: 200px;background-color: red;position: relative;left: -200px;top: 0;}
#box span{width: 20px;height: 50px;position: absolute;top: 50px;left: 200px;background-color: blue;}
</style>
<script type="text/javascript">
window.onload=function(){
var obox = document.getElementById('box');
obox.onmouseover = function(){
startmove(0);
}
obox.onmouseout = function(){
startmove(-200);
}
}
var timer = null;
function startmove(iTarget){
clearInterval(timer);
var obox = document.getElementById('box');
var speed = (iTarget-obox.offsetLeft)/20;
speed = speed > 0?Math.ceil(speed):Math.floor(speed);
timer = setInterval(function () {
if (obox.offsetLeft==iTarget) {
clearInterval(timer);
}else{
obox.style.left = obox.offsetLeft + speed+'px';
}
},30)
}
</script>
</head>
<body>
<div id="box"><span>分享</span></div>
</body>
</html>