|
| 1 | +<!Doctype > |
| 2 | +<html> |
| 3 | +<head> |
| 4 | +<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> |
| 5 | +<title>无标题</title> |
| 6 | +<style> |
| 7 | + #div{ |
| 8 | + width: 100px; |
| 9 | + height: 100px; |
| 10 | + border-radius: 50px; |
| 11 | + background: blue; |
| 12 | + position: absolute; |
| 13 | + } |
| 14 | + div{ |
| 15 | + width: 2px; |
| 16 | + height: 2px; |
| 17 | + position: absolute; |
| 18 | + background: black; |
| 19 | + } |
| 20 | +</style> |
| 21 | +</head> |
| 22 | +<body> |
| 23 | +<input id="button" value="开始运动" type="button"> |
| 24 | +<div id="div"></div> |
| 25 | +</body> |
| 26 | +<script> |
| 27 | +window.onload = function () |
| 28 | +{ |
| 29 | + var oBtn=document.getElementById('button'); |
| 30 | + var oDiv=document.getElementById('div'); |
| 31 | + oBtn.onclick=function(){ |
| 32 | + startMove(); |
| 33 | + } |
| 34 | + //拖拽 |
| 35 | + var lastX=0; |
| 36 | + var lastY=0; |
| 37 | + oDiv.onmousedown = function(ev) |
| 38 | + { |
| 39 | + var ev=ev||event; |
| 40 | + var disX=ev.clientX-oDiv.offsetLeft; |
| 41 | + var disY=ev.clientY-oDiv.offsetTop; |
| 42 | + document.onmousemove=function() |
| 43 | + { |
| 44 | + var ev=ev||event; |
| 45 | + var left2=ev.clientX-disX; |
| 46 | + var top2 =ev.clientY-disY; |
| 47 | + |
| 48 | + oDiv.style.left=left2+'px'; |
| 49 | + oDiv.style.top=top2+'px'; |
| 50 | + |
| 51 | + /*加黑点*/ |
| 52 | + // var oBox=document.createElement('div'); |
| 53 | + // oBox.style.left=left2+'px'; |
| 54 | + // oBox.style.top=top2+'px'; |
| 55 | + // document.body.appendChild(oBox); |
| 56 | + iSpeedX=left2 - lastX; |
| 57 | + iSpeedY=top2 - lastY; |
| 58 | + |
| 59 | + lastX=left2; |
| 60 | + lastY=top2; |
| 61 | + document.title='x:'+iSpeedX+'y:'+iSpeedY; |
| 62 | + } |
| 63 | + oDiv.onmouseup=function() |
| 64 | + { |
| 65 | + document.onmousemove=null; |
| 66 | + document.onmouseup=null; |
| 67 | + startMove(); |
| 68 | + }; |
| 69 | + clearInterval(timer); |
| 70 | + |
| 71 | + } |
| 72 | + |
| 73 | + |
| 74 | +} |
| 75 | + |
| 76 | + |
| 77 | +/*重力+碰撞*/ |
| 78 | +var iSpeedX=0; |
| 79 | +var iSpeedY=0; |
| 80 | +var timer=null; |
| 81 | +function startMove() |
| 82 | +{ |
| 83 | + clearInterval(timer); |
| 84 | + timer=setInterval(function(){ |
| 85 | + var oDiv=document.getElementById('div'); |
| 86 | + iSpeedY+=3; |
| 87 | + var left1=oDiv.offsetLeft + iSpeedX; |
| 88 | + var top1 = oDiv.offsetTop + iSpeedY; |
| 89 | + |
| 90 | + if(top1>=document.documentElement.clientHeight-oDiv.offsetHeight) |
| 91 | + { |
| 92 | + iSpeedY*=-0.8; |
| 93 | + iSpeedX*=0.8; |
| 94 | + top1=document.documentElement.clientHeight-oDiv.offsetHeight; |
| 95 | + |
| 96 | + } |
| 97 | + else if(top1<=0) |
| 98 | + { |
| 99 | + iSpeedY*=-1; |
| 100 | + iSpeedX*=0.8; |
| 101 | + top1=0; |
| 102 | + } |
| 103 | + if(left1>=document.documentElement.clientWidth-oDiv.offsetWidth) |
| 104 | + { |
| 105 | + iSpeedX*=-0.8; |
| 106 | + left1=document.documentElement.clientWidth-oDiv.offsetWidth; |
| 107 | + } |
| 108 | + else if(left1<=0) |
| 109 | + { |
| 110 | + iSpeedX*=-0.8; |
| 111 | + left1=0; |
| 112 | + } |
| 113 | + if(Math.abs(iSpeedX)<1) |
| 114 | + { |
| 115 | + iSpeedX=0; |
| 116 | + } |
| 117 | + if(Math.abs(iSpeedY)<1) |
| 118 | + { |
| 119 | + iSpeedY=0; |
| 120 | + } |
| 121 | + if(iSpeedY==0 && iSpeedX==0 && top1==document.documentElement.clientHeight-oDiv.offsetHeight) |
| 122 | + { |
| 123 | + clearInterval(timer); |
| 124 | + // alert(0); |
| 125 | + } |
| 126 | + else |
| 127 | + { |
| 128 | + oDiv.style.left=left1+'px'; |
| 129 | + oDiv.style.top =top1+'px'; |
| 130 | + } |
| 131 | + },30); |
| 132 | +} |
| 133 | + |
| 134 | +</script> |
| 135 | +</html> |
0 commit comments