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.