<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>js</title>
<style>
#div1{
width: 200px;
height:300px;
background-color: #2aabd2;
position: absolute;
}
#div2{
width: 100px;
height:200px;
background-color: #d28544;
position: absolute;
}
</style>
</head>
<body>
<div id="div1">原来的</div>
<div id="div2">继承的</div>
</body>
<script>
window.onload=function(){
new move1('div1');
new limitMove('div2');
};
function move1(id){
var _this=this;
this.oDiv=document.getElementById(id);
this.disX=0;
this.disY=0;
this.oDiv.onmousedown=function(ev){
_this.down(ev);
return false;
}
}
move1.prototype.down=function (ev){
var _this=this;
var oEvent=ev||event;
this.disX=oEvent.clientX-this.oDiv.offsetLeft;
this.disY=oEvent.clientY-this.oDiv.offsetTop;
document.onmousemove=function(ev){
_this.move(ev);
};
document.onmouseup=function(){
_this.up();
}
};
move1.prototype.move=function (ev){
var oEvent=ev||event;
this.oDiv.style.left=oEvent.clientX-this.disX+'px';
this.oDiv.style.top=oEvent.clientY-this.disY+'px';
};
move1.prototype.up=function () {
document.onmousemove=null;
document.onmouseup=null;
};
function limitMove(id){
move1.call(this,id);
}
for(var i in move1.prototype){
limitMove.prototype[i]=move1.prototype[i];
}
limitMove.prototype.move=function(ev){
var oEvent=ev||event;
var x=oEvent.clientX-this.disX;
var y=oEvent.clientY-this.disY;
if(x<50){
x=0;
}
else if(x>document.documentElement.offsetWidth-this.oDiv.offsetWidth){
x=document.documentElement.offsetWidth-this.oDiv.offsetWidth;
}
if(y<50){
y=0;
}
else if(y>document.documentElement.clientHeight-this.oDiv.offsetHeight){
y=document.documentElement.clientHeight-this.oDiv.offsetHeight;
}
this.oDiv.style.left=x+'px';
this.oDiv.style.top=y+'px';
}
</script>
</html>