从一个函数内部调用函数本身就叫做递归
function moveElement(elementID,final_x,final_y,interval){
var elem = document.getElementById(elementID);
var xpos = parseInt(elem.style.left);
var ypos = parseInt(elem.style.top);
if(xpos == final_x && ypos == final_y ) {
return true;
}
if(xpos<final_x) {
xpos++;
}
if(xpos>final_x) {
xpos--;
}
if(ypos <final_y) {
ypos++;
}
if(ypos>final_y) {
ypos--;
}
elem.style.left = xpos+"px";
elem.style.top = ypos+"px";
var repeat = "moveElement('"+elementID+"',"+final_x+","+final_y+","+interval+")";
setTimeout(repeat,interval)
}
和jQuery的animate()动画的效果相同