Analysis of JS motion basic framework instances

Source: Internet
Author: User

Analysis of JS motion basic framework instances

This article mainly introduces the basic framework of JS motion. examples show how to use javascript timers and div styles. For more information, see

 

 

This article describes the basic framework of JS motion. Share it with you for your reference. The specific analysis is as follows:

Note:

1. Disable the existing timer when starting the movement
2. Separate motion and stop

The Code is as follows:

<! DOCTYPE html>
<Html>
<Head>
<Meta charset = "UTF-8">
<Title> </title>
<Style type = "text/css">
# Div1 {
Width: 200px;
Height: 200px;
Background: red;
Position: absolute;
Left: 0;
Top: 60px;
}
</Style>
<Script type = "text/javascript">
Window. onload = function (){
Var oDiv = document. getElementById ("div1 ");
Var oBt = document. getElementsByTagName ('input') [0];
Var time = null;
OBt. onclick = function (){
ClearInterval (time); // first, you need to disable a timer. This is because the bug that multiple timer stacks are generated when you click the button multiple times during motion.
Time = setInterval (function (){
Var speed = 7;
If (oDiv. offsetLeft <= 600)
ODiv. style. left = oDiv. offsetLeft + speed + 'px ';
Else {
ClearInterval (time );
}
}, 30 );
}
}
</Script>
</Head>
<Body>
<Input type = "button" value = "Start motion"/>
<Div id = "div1"> </div>
</Body>
</Html>

 

I hope this article will help you design javascript programs.

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.