js拖拽面向對象

//關於 this問題指向,函數裏面還有函數的話,必須要把第一個函數的this保存下來給第二個函數訪問。

<script>

window.οnlοad=function(){
var t=new drag("box")
t.init();
}
function drag(name){
this.div=document.getElementById(name);
this.x;
this.y;
}
drag.prototype.init=function(){
var me=this;
this.div.οnmοusedοwn=function(e){
var e=e||event;
me.down(e);
}

}

drag.prototype.down=function(e){
this.x=e.clientX-this.div.offsetLeft;
this.y=e.clientY-this.div.offsetTop;
var me=this;
document.οnmοusemοve=function(e){
var e=e||event;
me.move(e);

}


document.οnmοuseup=function(){
me.up()
}
}

drag.prototype.move=function(e){
this.divx=e.clientX-this.x;
this.divy=e.clientY-this.y;
this.width=document.documentElement.clientWidth-this.div.offsetWidth;
this.height=document.documentElement.clientHeight-this.div.offsetHeight;
if(this.divx<0){this.divx=0}else if(this.divx>this.width){this.divx=this.width}
if(this.divy<0){this.divy=0}else if(this.divy>this.height){this.divy=this.height}
this.div.style.left=this.divx+"px"
this.div.style.top=this.divy+"px"
var me=this;



}

drag.prototype.up=function(){
document.οnmοuseup=null;
document.οnmοusemοve=null;
}


</script>
</head>


<body>
<div id="box" style=" width:100px;height:100px; background:#0F9; position:absolute"></div>




</body>
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章