鼠标拖拽练习

179 阅读1分钟

拖拽box1元素

拖拽的流程:

  1. 当鼠标在被拖拽元素上按下时,开始拖拽onmousedown
  2. 当鼠标移动时被拖拽元素跟随鼠标移动onmousgmove
  3. 当鼠标松开时,被拖拽元素固定在当前位置onmouseup
<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title></title>
		<style>
			#box1{
				width: 100px;
				height: 100px;
				background-color: red;
				position: absolute;
			}
			#box2{
				width: 100px;
				height: 100px;
				background-color: yellow;
				position: absolute;
				left: 200px;
				top: 200px;
			}
		</style>
	</head>
	<body>
		我是body的内容
		<div id="box1"></div>
		<div id="box2"></div>
	</body>
	<script>
		//获取box1
		var box1=document.getElementById("box1");
		//为box1绑定一个鼠标按下事件
		box1.onmousedown=function(event){
			//setCapture() 只有IE支持,火狐浏览器调用不报错,Chrome调用报错
			box1.setCapture && box1.setCapture();
			
			event=event || window.event;
			//div的偏移量 鼠标.clientX-box1.offsetLeft;
			//div的偏移量 鼠标.clientY-box1.offsetTop;
			var ol=event.clientX-box1.offsetLeft;
			var ot=event.clientY-box1.offsetTop;
			//为document绑定一个onmousemove事件
			//当鼠标在被拖拽元素上按下时,开始拖拽 onmousedown
			document.onmousemove=function(event){
				event=event || window.event;
				//当鼠标移动时被拖拽元素跟随鼠标移动onmousemove
				//获取鼠标的坐标
				var left=event.clientX-ol;
				var top=event.clientY-ot;
				//修改box1的位置
				box1.style.left=left+"px";
				box1.style.top=top+"px";
			};
			//为document绑定一个鼠标松开事件
			document.onmouseup=function(){
				//当鼠标松开时,被拖拽元素固定在当前位置 onmouseup
				//取消document的onmousemove事件
				document.onmousemove=null;
				
				//取消document的onmouseup事件
				document.onmouseup=null;
				
				//当鼠标松开时,取消对事件的捕获
				box1.releaseCapture && box1.releaseCapture();
			};
			//当我们拖拽一个网页中的内容时,浏览器会默认去搜索引擎中搜索内容,此时会导致拖拽功能的异常,这个是浏览器提供的默认行为,
			//如果不希望发生这个行为,则可以通过return false来取消默认行为
			//IE不能使用,用setCapture()
			return false;
		};
	</script>
</html>

将函数提取出来为drag()函数,可任意调用和拖拽元素

<!DOCTYPE html>
<html>

	<head>
		<meta charset="UTF-8">
		<title></title>
		<style>
			#box1 {
				width: 100px;
				height: 100px;
				background-color: red;
				position: absolute;
			}
			
			#box2 {
				width: 100px;
				height: 100px;
				background-color: yellow;
				position: absolute;
				left: 200px;
				top: 200px;
			}
		</style>
	</head>

	<body>
		我是body的内容
		<div id="box1"></div>
		<div id="box2"></div>
	</body>
	<script>
		function drag(obj) {
			obj.onmousedown = function(event) {
				obj.setCapture && obj.setCapture();

				event = event || window.event;

				var ol = event.clientX - obj.offsetLeft;
				var ot = event.clientY - obj.offsetTop;

				document.onmousemove = function(event) {
					event = event || window.event;

					var left = event.clientX - ol;
					var top = event.clientY - ot;

					obj.style.left = left + "px";
					obj.style.top = top + "px";
				};

				document.onmouseup = function() {

					document.onmousemove = null;


					document.onmouseup = null;

	
					box1.releaseCapture && box1.releaseCapture();
				};

				return false;
			};
		}
		//获取box1
		var box1 = document.getElementById("box1");
		//为box1绑定一个鼠标按下事件
		drag(box1);
		//获取box2
		var box2 = document.getElementById("box2");
		//为box2绑定一个鼠标按下事件
		drag(box2);
	</script>

</html>