14方块的随便移动

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title>正方体14</title>
		<style>
			*{
				margin: 0;
				padding: 0;
			}
			body{
				position: absolute;
				top: 0;
				width: 100%;
			}
			#box{
				width: 200px;
				height: 200px;
				background-color: aqua;
				position: absolute;
				top: 100px;
				left: 200px;
			}
		</style>
	</head>
	<body>
		<div id="box">
		</div>
		<script>
			var box=document.getElementById("box");
			var flag=0;
			var offx,offy;
			box.onmousedown=function(e){
				flag=1;
				var boxrect=box.getBoundingClientRect();
				offx=e.x - boxrect.left;
				offy=e.y - boxrect.top;
			}
			box.onmousemove=function(e){
				if(flag==0) return;
				box.style.left=(e.x - offx)+"px";
				box.style.top=(e.y - offy)+"px";
			};
		</script>
	</body>
</html>

  

posted @ 2020-11-27 08:21  卿家尘归尘  阅读(48)  评论(0编辑  收藏  举报