Slow Movements of Elements
Slow Movements of Elements
With JavaScript we can not only move the elements instantly, we can also move
element slowly from one place to another. The one way to move an element slowly is to
move it by small amount many times, with moves separated by small amounts of time.
Such slow movement of elements is accomplished by using two window methods
available in JavaScript: setTimeout and setInterval.
The setTimeout method has two parameters -- first one is string of JavaScript code to
be executed and second parameter is number of mseconds of delay before executing the
given script.
The setInterval method has two forms. One takes two parameter with first specifying
the script code to be executed and second specifying the interval in mseconds between
executions. The second form takes variable number of parameters. First one is the name
of the function to be called, the second is the interval in mseconds between the calls to
the function and remaining parameter are used as actual parameters to the function being
called.
Initial position of the element is set with initImage( ) function. The onload attribute of
the body element is used to call this function.
The moveImage( ) function is used to move the image. It moves the image 1 pixel
towards the final position and then calls itself with the new coordinates using the
setTimeout.
function initImage() {
dom = document.getElementById('image').style;
function moveImage(x, y) {
if (x != finalx)
x++;
if ((x != finalx) ) {
dom.left = x + "px";
dom.top = y + "px";
Source : http://elearningatria.files.wordpress.com/2013/10/cse-vii-programming-the-web-10cs73-
notes.pdf