function Drag(id) { var _this=this; this.disX=0; this.disY=0; this.oDiv=document.getElementById(id); this.oDiv.onmousedown=function(e) { _this.fnDown(e); } } Drag.prototype.fnDown=function(e) { var _this=this; var e=e||event; this.disX=e.pageX-this.oDiv.offsetLeft; this.disY=e.pageY-this.oDiv.offsetTop; document.onmousemove=function(e) { _this.fnMove(e); } document.onmouseup=function() { _this.fnUp(); } } Drag.prototype.fnMove=function(e) { var e=e||event; this.oDiv.style.left=e.pageX-this.disX+‘px‘; this.oDiv.style.top=e.pageY-this.disY+‘px‘; } Drag.prototype.fnUp=function() { document.onmousemove=null; document.onmouseup=null; } function Drag2(id) { Drag.call(this,id); } for(var i in Drag.prototype) { Drag2.prototype[i]=Drag.prototype[i]; } window.onload=function(){ var a = new Drag(‘div1‘); var b = new Drag2(‘div2‘); }
Javascript面向对象拖拽,布布扣,bubuko.com
原文:http://www.cnblogs.com/anson0415/p/3891338.html