可拖拽的弹性碰撞小球

实现鼠标拖拽、根据拖拽的速度来控制小球的速度,然后进行碰撞检测,碰撞过程中会有速度的损耗。代码如下C+V即可看效果 吐舌头
<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>demo1</title>
	<style>
	body{
		
		margin: 0px;
		padding: 0px;
		height: 630px;
		font-family: "微软雅黑";
		background: black;	
	}
	#wind{
		width: 900px;
		height: 600px;
		margin-left: 200px;
		background: pink;
		position: relative;
	}
	#it{
		position: absolute;
		width: 50px;
		height: 50px;
		left: 420px;
		top: 100px;
		border-radius: 25px;
		background: green;
		opacity: 1;
	}
	</style>
</head>
<body>
<div id="wind">
	<div id="it">		
	</div>
</div>
	<script>
	window.οnlοad=function(){
		var aBody=document.getElementById('wind');
		var aDiv=document.getElementById('it');
		var aDivx=0;
		var aDivy=0;
		var arr=[];
		var bBt=true;
		aDiv.οnmοusedοwn=function(ev){
			var ev=ev||window.event;
			aDivx=ev.clientX-aDiv.offsetLeft;
			aDivy=ev.clientY-aDiv.offsetTop;
			speed0=ev.clientX;
			speed1=ev.clientY;
			document.οnmοusemοve=function(ev){
				var ev=ev||window.event;			
                aDiv.style.left=ev.clientX-aDivx+'px';
                aDiv.style.top=ev.clientY-aDivy+'px';
            speedX=ev.clientX-speed0;
            speedY=ev.clientY-speed1;
            speed0=ev.clientX;
            speed1=ev.clientY;       
			}
			document.οnmοuseup=function(){
              document.οnmοusemοve=null;
               move();              
			}
		}
		function move(){
		   var aDivX=aDiv.offsetLeft;
               var aDivY=aDiv.offsetTop; 
              if (aDivY<aBody.offsetHeight-aDiv.offsetHeight) {
              	 var speed=speedY;
              	 var speedXX=speedX; 
                 var timer=null;
               clearInterval(timer);
               timer=setInterval(function(){
              	aDiv.style.top=aDiv.offsetTop+speed+'px';
                aDiv.style.left=aDiv.offsetLeft+speedXX+'px';
                speed+=6;   
               	if (aDiv.offsetTop>=aBody.offsetHeight-aDiv.offsetHeight) 
               	{
               		aDiv.style.top=aBody.offsetHeight-aDiv.offsetHeight+'px';
               		speed*=-1;
               		speed*=0.5;
               		speedXX*=0.75;		
               	}else if(aDiv.offsetTop<=0){
                   aDiv.style.top=0+'px';
               		speed*=-1;
               		speed*=1.5;
               		speedXX*=0.8;
               	}
               	if (aDiv.offsetLeft<=0) 
               	{
               		aDiv.style.left=0+'px';
               		speedXX*=-1;
               		speedXX*=0.8; 
               	}else if(aDiv.offsetLeft>=aBody.offsetWidth-50){
               		aDiv.style.left=aBody.offsetWidth-50+'px';
               		speedXX*=-1;
               		speedXX*=0.75;
               	}
                if (aDiv.offsetTop==aBody.offsetHeight-aDiv.offsetHeight&&speed>-10) {
                	speed=0;
                	if(Math.abs(speedXX)<=1){
                		clearInterval(timer);
                	}
                }
               },100);
              }
	}
	}
	</script>
</body>
</html>


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值