<!DOCTYPE HTML Public "-//W3C//DTD XHTML 1.0 transitional//en" "http://www.w3.org/TR/xhtml1/DTD/ Xhtml1-transitional.dtd "><HTMLxmlns= "http://www.w3.org/1999/xhtml"><Head> <title>Collision Motion</title> <style>#div1{width:100px;Height:100px;background:Red;position:Absolute; } </style> <Scripttype= "Text/javascript">window.onload= function(){ varobtn=document.getElementById ("btn"); Obtn.onclick= function() {move (); } } varTimer= NULL; varSpeedx= 6; varSpeedY= 8; functionMove () {clearinterval (timer); Timer=SetInterval (function(){ varOdiv=document.getElementById ("Div1"); varL=Odiv.offsetleft+Speedx; varT=Odiv.offsettop+SpeedY; if(t>=Document.documentElement.clientHeight-odiv.offsetheight) {SpeedY*=-1; T=Document.documentElement.clientHeight-Odiv.offsetheight; } Else if(t<=0) {SpeedY*=-1; T=0; } if(L>=Document.documentElement.clientWidth-odiv.offsetwidth) {Speedx*=-1; L=Document.documentElement.clientWidth-Odiv.offsetwidth; } Else if(L<=0) {Speedx*=-1; L= 0; } oDiv.style.left=L+'px'; ODiv.style.top=T+'px'; }, -); } </Script></Head><Body> <inputtype= "button"ID= "BTN"value= "movement" /> <DivID= "Div1"> </Div> <spanstyle= "width:1px;height:300px;background:black;left:300px"></span></Body></HTML>
JS Collision Motion