DOM事件 --鼠标拖拽div

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u011344924/article/details/48006945


实现效果:



分析:

1.当鼠标在粉色div上按下时,捕获onmousedown事件,获取鼠标的坐标clientX,clientY

2.鼠标在按下后移动,整个div跟随着移动,获取鼠标按下时相对于整个div的坐标,即下图中的disx,dixy

3.鼠标抬起后,整个div不再随鼠标移动,onmouseup事件

4.设置整个div能够活动的范围,即浏览器窗口大小减去div本身所占大小




具体代码:

<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8"/>
	<title></title>
	<style type="text/css">
	body{margin:0;padding:0;}
	#wrap{width:300px;height:200px;border:1px solid #FFFFCC;background-color:#CCFFFF;position:absolute;}
	#moveHere{cursor: move;width:300px;height: 30px;text-align: center;line-height:30px;background-color:#FFCCCC;}
	</style>
</head>
<body>
<div id="wrap">
<div id="moveHere"></div>
</div>
<script type="text/javascript">
var moveHere=document.getElementById("moveHere");
var wrap=document.getElementById("wrap");
var winW=document.documentElement.clientWidth || document.body.clientWidth;
var winH=document.documentElement.clientHeight || document.body.clientHeight;
maxW=winW-wrap.offsetWidth;
maxH=winH-wrap.offsetHeight;
window.onload=function(){
	wrap.style.left=(maxW/2)+"px";
	wrap.style.top=(maxH/2)+"px";
	moveHere.onmousedown=mousedown;
	moveHere.onmouseup=mouseup;
}
function mousedown(event){
	event=event||window.event;
	var disx = event.clientX-wrap.offsetLeft;
	var disy = event.clientY-wrap.offsetTop;
	document.onmousemove = function(event){
		event=event||window.event;
		move(event,disx,disy);
	}	
}
function move(event,disx,disy){
	var x = event.clientX-disx;
	var y = event.clientY-disy;
	if(x<0){
		x=0;
	}else if(x>maxW){
		x=maxW;
	}
	if(y<0){
		y=0;
	}else if(y>maxH){
		y=maxH;
	}
	wrap.style.left=x+"px";
	wrap.style.top=y+"px";
}
function mouseup(){
	document.onmousemove=null;
	document.onmouseup=null;
}
</script>
</body>
</html>


猜你喜欢

转载自blog.csdn.net/u011344924/article/details/48006945