1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
| function moveElement(elementId, final_x, final_y, interval) { if (!document.getElementById) return false; if (!document.getElementById(elementId)) return false; let elem = document.getElementById(elementId); let xpos = parseInt(elem.style.left); let ypos = parseInt(elem.style.top); let dist = 0;
if (elem.movement) { clearTimeout(elem.movement) } if (!elem.style.left) elem.style.left = "0px"; if (!elem.style.top) elem.style.top = "0px"; if (xpos == final_x && ypos == final_y) { return true; } if (xpos < final_x) { dist = Math.ceil((final_x - xpos) / 10); xpos += dist; } if (xpos > final_x) { dist = Math.ceil((xpos - final_x) / 10); xpos -= dist; } if (ypos < final_y) { dist = Math.ceil((final_y - ypos) / 10); ypos += dist; } if (ypos > final_y) { dist = Math.ceil((ypos - final_y) / 10); ypos -= dist; } elem.style.left = xpos + 'px'; elem.style.top = ypos + 'px';
let repeat = `moveElement('${elementId}', ${final_x}, ${final_y}, ${interval})`; elem.movement = setTimeout(repeat, interval); }
|