Uniform motion, how to stop it when it arrives at the specified position?
Principle:
1, the difference between the object and the target distance is less than or equal to the speed, that is, stop
2, then move the object position equal to the target position
Example: Uniform motion Stop
HTML section
<input type= "button" value= "100 m" id= "btn1" onclick= "startmove (+);"/><input type= "button" value= "300 m" id= "Btn2" onclick= "Startmove", "/><div id=" Div1 "></div><div id=" Div2 "></div><div id= "Div3" ></div><style> #div1 {position:absolute; left:600px; width:100px; height:150px; background:red;} #div2 {position:absolute; left:300px; height:800px; width:1px; background:black;} #div3 {position:absolute; left:100px; height:800px; width:1px; background:black;} </style>
JS section:
<script>var timer = null;function Startmove (itarget) {var odiv = document.getElementById ("Div1"); var speed; Clearinterval (timer); timer = setinterval (function () {if (odiv.offsetleft<itarget) {speed = 7;} else {speed =-7;} if (Math.Abs (itarget-odiv.offsetleft) <= 7) {clearinterval (timer); oDiv.style.left = ITarget + "px";} else {Odiv.style . left = Odiv.offsetleft + speed + "px";}},30);} </script>