标签:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>匀速运动结束条件</title> <style type="text/css"> #div{ width: 100px; height: 100px; background: red; position: absolute; top:50px; left: 0px; } </style> <script type="text/javascript"> window.onload=function(){ var oBtn=document.getElementById("btn"); var oDiv=document.getElementById("div"); var oBtn2=document.getElementById("btn2"); oBtn.onclick=function(){//前进 move(oDiv,509); }; oBtn2.onclick=function(){//后退 move(oDiv,20); }; var oTimer=null;//定时器 function move(obj,iTarget){//运动函数:obj:运动对象,iTarget运动的距离 clearInterval(oTimer); oTimer=setInterval(function(){ var iSpeed=0;//速度 if (iTarget>obj.offsetLeft) { iSpeed=12; }else{ iSpeed=-12; }; if (Math.abs(obj.offsetLeft-iTarget)<10) { clearInterval(oTimer); obj.style.left=iTarget+"px";//处理匀速运动前后差距 } else{ obj.style.left=obj.offsetLeft+iSpeed+"px";//运动 }; },30); } }; </script> </head> <body> <input type="button" id="btn" value="forward move" /> <input type="button" id="btn2" value="back move" /> <div id="div"> </div> </body> </html>
标签:
原文地址:http://www.cnblogs.com/mengfanrong/p/5071227.html