Javascript面向对象拖拽

function Drag(id)
{    
    var _this=this;
    this.disX=0;
    this.disY=0;    
    this.oDiv=document.getElementById(id);
    this.oDiv.οnmοusedοwn=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');
}

 

转载于:https://www.cnblogs.com/anson0415/p/3891338.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值