日期:2014-05-16  浏览次数:20383 次

javascript简单的拖拉效果。
<!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=utf-8" />
<title>简单的拖拉机</title>
</head>

<body>
<div id="test" style="border:1px solid; width:400px; background:#CCCCCC;">
  <p>把我拖一下</p>
  <p>试下</p>
</div>  
<script type="text/javascript">
var test=document.getElementById("test");
var mouseX,mouseY,top,left;
var Action=false;
//定义鼠标放下,移动,松开的动作
var Control={down:function(event){
		Action=true;
		event=event||window.event;
		mouseX=parseInt(event.clientX);
		mouseY=parseInt(event.clientY);
		top=parseInt(test.style.top);
		left=parseInt(test.style.left);
		},
		move:function(event){
		event=event||window.event;
		if(Action==true){
		var x=parseInt(event.clientX);
		var y=parseInt(event.clientY);
		test.style.left=(x-mouseX+left)+"px";
		test.style.top=(y-mouseY+top)+"px";
		}
	},
	up:function(){
		Action=false;
		}	
	}	
window.onload=function(){
	test.style.position="absolute";
	test.style.top=0+"px";
	test.style.left=0+"px";
	test.onmousedown=Control.down;
	test.onmousemove=Control.move;
	test.onmouseup=Control.up;
	}	
</script>
</body>
</html>
?