<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title></title>
<style>
#div1 {
height:100px;
width:100px;
background-color:#0094ff;
position:absolute;
}
</style>
</head>
<body>
<div id="div1"></div>
<div id="div2"></div>
<script>
var div1 = document.getElementById("div1");
var div2 = document.getElementById("div2");
div1.onmousedown=function(e) {
var er=e||event;
var disx=er.offsetX;
var disy = er.offsetY;
div1.onmousemove=function(e1) {
var er1 = e1 || event;
var x = er1.clientX - disx;
var y = er1.clientY - disy; div1.style.left = x + "px";
div1.style.top = y + "px";
}
}
div1.onmouseup = function () {
div1.onmousemove = null;
}
</script>
</body>
</html>
04-27 23:03