JQuery拖拽+自由落体实现

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>drag+drop</title>
<script type="text/javascript" src="jquery-1.6.3.js"></script>
<script type="text/javascript">
function GravityObj(idORclass){
     var obj=this;
	 this.idORclass=idORclass;
     this.velocity=1000;
	 this.gravity=1000;
	 this.longTopx=0.0003;
	 this.stopNow=null;
	 this.bottom=0;
	 this.tAnimate=1;
	 this.hit=0.9;
	 this.deltaX=0;
	 this.deltaY=0;
	 this.helpStop=false;
	 function dragStart(dragEvent){
							 obj.deltaX=dragEvent.clientX-$(obj.idORclass).offset().left;
							 obj.deltaY=dragEvent.clientY-$(obj.idORclass).offset().top;
//****************************PS:这里用document很关键。如果你用的还是像别的一样$(dragObj.idORclass),拖动时会
//*******************************一卡卡的,因为拖动的物体可能跟不上你鼠标的移动速度。
                             $(obj.idORclass).unbind("mouseup");
							 $(document).bind("mousemove",dragMove);  
							 $(obj.idORclass).bind("mouseup",dragStop);
							 $(obj.idORclass).bind("mouseup",dropStart);							 	                         
	 }
	 function dragMove(dragEvent){
	                         $(obj.idORclass).css({
													 "left":(dragEvent.clientX-obj.deltaX)+"px",
													 "top" :(dragEvent.clientY-obj.deltaY)+"px",
		                                          }) 
	 }
	 function dragStop(){
	                        // alert("dragStop");
							 $(document).unbind("mousemove",dragMove);
							 $(obj.idORclass).unbind("mouseup",dragStop);
							 var rBottom=getBottom(obj.idORclass);
							 $(obj.idORclass).css("top","auto");
							 $(obj.idORclass).css("bottom",rBottom+"px");
							 obj.bottom=rBottom;
							 obj.velocity=0;
							// alert(rBottom);
	 }
	 function dropStart(){
	                         //alert("dropStart"+obj.idORclass);
							 //event.stopPropagation();
							 //alert(obj.bottom);
							 //alert(obj.velocity);
							 dropMove();
							 $(obj.idORclass).unbind("mouseup",dropStart);	
	 }
	 function dropMove(){
	                         obj.stopNow=setInterval(dropAction(),obj.tAnimate);
	 }
	 function dropStop(){
	                         //alert("dropStop"+obj.idORclass);
							 clearInterval(obj.stopNow);
							 return
	 }
	 function getBottom(idORclass){
							 return rBottom=document.documentElement.clientHeight-$(idORclass).offset().top-parseInt($(idORclass).css("height"));
	 }
	 function dropAction(){
						     return function(){
												 var positionChange=(obj.velocity*obj.tAnimate+(1/2)*obj.gravity*obj.tAnimate*obj.tAnimate)*obj.longTopx;
												   //  ds=v0*t+(1/2)g(t^2)  
												 obj.velocity=obj.velocity+obj.gravity*obj.tAnimate;       //v=v0+gt
												 obj.bottom=obj.bottom-positionChange;
												 var endbottom=5;
												 if(obj.bottom<=endbottom){
																  obj.velocity=(0-obj.velocity)*obj.hit;                  //碰撞后速度的变化
																  if (Math.abs(obj.velocity)<=100){
																					 dropStop();
																					 return;
																  }
												 }		
												 $("#output").html("速度"+obj.velocity+"高度"+obj.bottom);	 
												 $(obj.idORclass).animate({bottom:obj.bottom+"px"},obj.tAnimate);
						     }
     }
	 function mousedownBind(){
		                     $(obj.idORclass).bind("mousedown",dragStart);
		                     $(obj.idORclass).bind("mousedown",dropStop);
	 }
	 $(document).ready(function(){
	                         mousedownBind();
	 })
}
var obj1=new GravityObj(".gravity1");
var obj2=new GravityObj(".gravity2");
</script>
<style type="text/css">
body{
margin:0;
padding:0;
}
.gravity1{
height:100px;
width:100px;
background-color:#000066;
position:absolute;
cursor:pointer;
}
.gravity2{
height:100px;
width:100px;
right:200px;
background-color:red;
position:absolute;
cursor:pointer;
}

</style>
</head>

<body>
<div class="gravity1"></div>
<div class="gravity2"></div>
<span id="output"></span>
</body>
</html>


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值