<!DOCTYPE HTML><HTMLLang= "en"><Head> <MetaCharSet= "UTF-8"> <title>Div move</title> <style>Div{width:100px;Height:100px;Background-color:Green;position:Absolute; Left:0;Top:0;}input{Margin-top:120px;} </style></Head><Body> <DivID= "Div1"></Div> <inputtype= "button"value= "Start Motion"onclick= "Startmove ()"></Body></HTML><Script> var Time= NULL; functionStartmove () {varOdiv=document.getElementById ("Div1"); Clearinterval (time); time=SetInterval (function () { varspend= 2; if(Odiv.offsetleft>= -){//whether to reach the endclearinterval (time);//after reaching the finish line }Else{oDiv.style.left=Odiv.offsetleft+spend+"px";//before you arrive } }, -); }</Script>
Thinking:
Will not stop, if () resolved;
Speed to take certain values when the time can not stop, pay attention to the condition ==300, the speed of 7 can not be stopped, resolved: Select the interval range;
When we reach 300, we also exercise. else solution;
Repeated clicks speed up, this is because each click, has opened a timer, so at the beginning of the event will be added clearinterval (time);
Sport (i)