Use JavaScript to achieve animation Effects

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.