<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>拖拽之面向对象</title>
<style>
#box1{ 100px;height: 100px;background-color: #0A246A;
position: absolute;}
</style>
<script>
//第二步:全局变量
var oDiv=null;
var disX=0;
var disY=0;
window.onload=function(){
oDiv=document.getElementById("box1");
oDiv.onmousedown=fnDown;
}
//第一步:拎嵌套函数
function fnDown(ev){
var oEvent=ev||event;
var disX = oEvent.clientX-oDiv.offsetLeft;
var disY = oEvent.clientY-oDiv.offsetTop;
document.onmousemove=fnMove
document.onmouseup=fnUp
}
function fnMove(ev){
var oEvent=ev||event;
oDiv.style.left=oEvent.clientX-disX+'px';
oDiv.style.top=oEvent.clientY-disY+'px';
}
function fnUp(){
document.onmousemove=null;
document.onmouseup=null;
}
</script>
</head>
<body>
<div id="box1"></div>
</body>
</html>