鼠标拖拽

1.拖拽流程

拖拽box1元素
			 *  - 拖拽的流程
			 * 		1.当鼠标在被拖拽元素上按下时,开始拖拽  onmousedown
			 * 		2.当鼠标移动时被拖拽元素跟随鼠标移动 onmousemove
			 * 		3.当鼠标松开时,被拖拽元素固定在当前位置	onmouseup

1.2

相当于加了if判断,如果前面有就执行,没有就不执行。
box1.setCapture && box1.setCapture();
当我们拖拽一个网页中的内容时,浏览器会默认去搜索引擎中搜索内容,
* 	此时会导致拖拽功能的异常,这个是浏览器提供的默认行为,
* 	如果不希望发生这个行为,则可以通过return false来取消默认行为
* 
* 但是这招对IE8不起作用
* 要对付ie8,要在按下鼠标后设置setCapture();来捕获所有按下的事件,当然在松开鼠标后要取消捕获

2.代码

看这一段就行

<script type="text/javascript">
			/*
			 * 拖拽box1元素
			 *  - 拖拽的流程
			 * 		1.当鼠标在被拖拽元素上按下时,开始拖拽  onmousedown
			 * 		2.当鼠标移动时被拖拽元素跟随鼠标移动 onmousemove
			 * 		3.当鼠标松开时,被拖拽元素固定在当前位置	onmouseup
			 */
			var box1 = document.getElementById("box1");
			box1.onmousedown = function(event) {
				event = event || window.event;
				//当鼠标移动时被拖拽元素跟随鼠标移动 onmousemove
				//获取鼠标的坐标
				
				//点的时候距离就定了
				var ol = event.clientX - box1.offsetLeft; 
				var ot = event.clientY - box1.offsetTop;
				
				//相当于加了if判断,如果前面有就执行,没有就不执行。
				box1.setCapture && box1.setCapture();
				document.onmousemove = function(event) {

					event = event || window.event;
					let pox = event.clientX;
					let poy = event.clientY;
					box1.style.left = pox-ol + "px";
					box1.style.top = poy-ot + "px";
				};
				document.onmouseup = function(event){
					event = event||window.event;
					//当鼠标松开时,被拖拽元素固定在当前位置	onmouseup
					//取消document的onmousemove事件
					document.onmousemove = null;
					//当鼠标松开时,取消对事件的捕获
					document.onmouseup = null;
					box1.releaseCapture && box1.releaseCapture();
				}
				/*
				 * 当我们拖拽一个网页中的内容时,浏览器会默认去搜索引擎中搜索内容,
				 * 	此时会导致拖拽功能的异常,这个是浏览器提供的默认行为,
				 * 	如果不希望发生这个行为,则可以通过return false来取消默认行为
				 * 
				 * 但是这招对IE8不起作用
				 */
				return false;
			};
		</script>

下面提取成了函数可以不看

<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title></title>
		<style type="text/css">
			
			#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>
		
		<script type="text/javascript">
			
			window.onload = function(){
				/*
				 * 拖拽box1元素
				 *  - 拖拽的流程
				 * 		1.当鼠标在被拖拽元素上按下时,开始拖拽  onmousedown
				 * 		2.当鼠标移动时被拖拽元素跟随鼠标移动 onmousemove
				 * 		3.当鼠标松开时,被拖拽元素固定在当前位置	onmouseup
				 */
				
				//获取box1
				var box1 = document.getElementById("box1");
				var box2 = document.getElementById("box2");
				var img1 = document.getElementById("img1");
				
				//开启box1的拖拽
				drag(box1);
				//开启box2的
				drag(box2);
				
				drag(img1);
				
				
				
				
			};
			
			/*
			 * 提取一个专门用来设置拖拽的函数
			 * 参数:开启拖拽的元素
			 */
			function drag(obj){
				//当鼠标在被拖拽元素上按下时,开始拖拽  onmousedown
				obj.onmousedown = function(event){
					
					//设置box1捕获所有鼠标按下的事件
					/*
					 * setCapture()
					 * 	- 只有IE支持,但是在火狐中调用时不会报错,
					 * 		而如果使用chrome调用,会报错
					 */
					/*if(box1.setCapture){
						box1.setCapture();
					}*/
					obj.setCapture && obj.setCapture();
					
					
					event = event || window.event;
					//div的偏移量 鼠标.clentX - 元素.offsetLeft
					//div的偏移量 鼠标.clentY - 元素.offsetTop
					var ol = event.clientX - obj.offsetLeft;
					var ot = event.clientY - obj.offsetTop;
					
					
					//为document绑定一个onmousemove事件
					document.onmousemove = function(event){
						event = event || window.event;
						//当鼠标移动时被拖拽元素跟随鼠标移动 onmousemove
						//获取鼠标的坐标
						var left = event.clientX - ol;
						var top = event.clientY - ot;
						
						//修改box1的位置
						obj.style.left = left+"px";
						obj.style.top = top+"px";
						
					};
					
					//为document绑定一个鼠标松开事件
					document.onmouseup = function(){
						//当鼠标松开时,被拖拽元素固定在当前位置	onmouseup
						//取消document的onmousemove事件
						document.onmousemove = null;
						//取消document的onmouseup事件
						document.onmouseup = null;
						//当鼠标松开时,取消对事件的捕获
						obj.releaseCapture && obj.releaseCapture();
					};
					
					/*
					 * 当我们拖拽一个网页中的内容时,浏览器会默认去搜索引擎中搜索内容,
					 * 	此时会导致拖拽功能的异常,这个是浏览器提供的默认行为,
					 * 	如果不希望发生这个行为,则可以通过return false来取消默认行为
					 * 
					 * 但是这招对IE8不起作用
					 */
					return false;
					
				};
			}
			
			
		</script>
	</head>
	<body>
		
		我是一段文字
		
		<div id="box1"></div>
		
		<div id="box2"></div>
		
		<img src="img/an.jpg" id="img1" style="position: absolute;"/>
	</body>
</html>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值