When you use the timeline function in Dreamweaver or make interesting animations, Dreamweaver will automatically generate specific program code for you. Have you ever thought about how the animation works? In fact, the principle is very simple, mainly using a timer function. Below I will give you
This article describes how to create a simple animation. You can introduce it to others to make more dazzling animations.
The effect of this instance is to click the "start to move" button on the webpage, and the specified layer will be moved from left to right, in this process, you click "Stop moving" to stop moving.
<Html>
<Head>
<Title> JavaScript Motion Sample </title>
<Script language = "JavaScript">
Var movingID = null;
Var scrolling = false;
Function startMove ()
{
Var left = eval (div1.style. left. replace ("px ",""));
If (left <document. body. scrollWidth-150)
Div1.style. left = left + 1;
Else
Div1.style. left = 10;
MovingID = setTimeout ("startMove ()", 10 );
}
Function stopMove ()
{
ClearTimeout (movingID );
}
</Script>
</Head>
<Body>
<Div id = "div1" style = "visibility: visible; position: absolute; left: 10; top: 10; z-index: 1;">
<Table bgColor = "# FFFFCC" border = "1" cellPadding = "0" cellSpacing = "0">
<Tr>
<Td> I can moving... </td>
</Tr>
</Table>
</Div>
<Br>
<Input type = "button" value = "start to move" onClick = "startMove ()">
<Input type = "button" value = "Stop moving" onClick = "stopMove ()">
</Body>
</Html>
Here we mainly use a function called setTimeout (function, interval). The parameter format of this function is:
The first parameter "function" is the name of the function called after timeout, and the second parameter "interval" is the timeout value, in microseconds.
Note that to stop the timer, you must save the returned value after calling the setTimeout () function and use the clearTimeout (id) function to cancel the timer.