window.onload = function () {
var oDiv = document.getElementById('div1');
oDiv.onmouseover = function () {
startMove(7,0);
}
oDiv.onmouseout = function () {
startMove(-10,-200);
}
}
var timer = null;
// 传参数的方法
function startMove(speed,iTarget) {
var oDiv = document.getElementById('div1');
// 防止多次启用定时器
clearInterval(timer);
// 添加一个定时器
timer = setInterval(function () {
// 判断目标值
if (oDiv.offsetLeft === iTarget) {
clearInterval(timer);
} else {
// 给当前位置的值加10
oDiv.style.left = oDiv.offsetLeft + speed + 'px';
}
}, 30);
}