Blogger Information
Blog 250
fans 3
comment 0
visits 321560
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
js实例-拖拽方格
梁凯达的博客
Original
734 people have browsed it

实例

<!DOCTYPE html>
<html lang="zh">
<head>
	<meta charset="UTF-8" />
	<meta name="viewport" content="width=device-width, initial-scale=1.0" />
	<meta http-equiv="X-UA-Compatible" content="ie=edge" />
	<title>Document</title>
	<style type="text/css">
		*{
			padding: 0px;
			margin: 0px;
		}
		#box{
			width: 100px;
			height: 100px;
			background: deeppink;
			position: absolute;
		}
	</style>
</head>
<body>
	<div id="box" style="left:0px;top:0px"></div>
</body>
	<script type="text/javascript">
		//找对象
		var box = document.getElementById('box');
		//增加鼠标按下对象
		box.onmousedown = function(e){
			var e = e||event;
			//获取X轴
			var y = e.clientY - parseInt(box.style.top);

			var x = e.clientX - parseInt(box.style.left);
			console.log(y);
			console.log(x);
			//保证兼容性问题
			document.onmousemove = function(e){
				var e = e||event;
				//修改box的坐标
				document.tite = "X:"+e.clientX+"Y:"+e.clientY;
				box.style.left = e.clientX - x + 'px';
				box.style.top = e.clientY - y + 'px';

			}
		}
		box.onmouseout = function(){
			document.onmousemove = null;
		}
	</script>
</html>

运行实例 »

点击 "运行实例" 按钮查看在线实例

Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post