面向对象的简单拖拽

Posted on 2018-05-09 13:39  凌晨四点的北京  阅读(212)  评论(0)    收藏  举报

<!DOCTYPE html>

<html>

<head>

<meta charset="utf-8" />

<style>

#aaa{

width: 100px;

height: 100px;

background: red;

position: absolute;

}

#dddd{

width: 100px;

height: 100px;

background: yellow;

position: absolute;

top:20px;

left: 500px;

}

</style>

</head>

<body>

<div id='aaa'></div>

<div id='dddd'></div>

<script>

function Drawele(id){

var that=this

this.obj=document.getElementById(id),

this.initX=0,

this.initY=0,

this.obj.onmousedown=function(e){

var e=e||window.event;

that.mouseDowntime(e);

document.onmousemove=function(e){

var e=e||window.event;

that.mouseMovetime(e)

}

document.onmouseup=function(e){

var e=e||window.event;

that.mouseup(e)

}

}

}

Drawele.prototype.mouseDowntime=function(event){

this.initX=event.clientX-this.obj.offsetLeft,

this.initY=event.clientY-this.obj.offsetTop

}

Drawele.prototype.mouseMovetime=function(event){

this.obj.style.left=event.clientX-this.initX+'px';

this.obj.style.top=event.clientY-this.initY+'px'

}

Drawele.prototype.mouseup=function(){

document.onmousemove=null;

}

 

var a=new Drawele('aaa')

var b=new Drawele('dddd')

</script>

</body>

</html>

博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3