var btns=document.getElementsByTagName('button')[0]; var bodys=document.getElementsByTagName('body')[0]; function Box() { this.width=100; this.height=100; this.creatBox(); } Box.prototype.ranColor=function(){ return Math.floor(Math.random()*255); } Box.prototype.creatBox=function () { this.ele=document.createElement('div'); this.ele.style.cssText='width:'+this.width+'px;height:'+this.height+'px;background:rgb('+this.ranColor()+","+this.ranColor()+","+this.ranColor()+");"; bodys.append(this.ele); this.resize(); } Box.prototype.resize=function () { var that=this; this.ele.onmousedown=function (ev) { var boxX=ev.clientX-this.offsetLeft; var boxY=ev.clientY-this.offsetTop; document.onmousemove=function (ev) { that.ele.style.cssText+='left:'+(ev.clientX-boxX)+'px;top:'+(ev.clientY-boxY)+'px'; } document.onmouseup=function () { document.onmouseup=document.onmousemove=null; } } } btns.onclick=function () { var a=new Box(); }
|