js组件开发流程

html代码

<div id="div1"></div>
<div id="div2"></div>
<div id="div3"></div>

 

css样式

#div1{
    width: 100px;
    height: 100px;
    background: red;
    cursor: move;
    position:absolute;
    left:0;
    top: 0;
}
#div2{
    width: 100px;
    height: 100px;
    background: black;
    cursor: move;
    position:absolute;
    left:100px;
    top: 0;
}
#div3{
    width: 100px;
    height: 100px;
    background: blue;
    cursor: move;
    position:absolute;
    left:200px;
    top: 0;
}

 

js代码

<script>
window.onload=function(){
    var oDiv1=new Drag();
    oDiv1.init({
        id:'div1'
    });

    var oDiv2=new Drag();
    oDiv2.init({
        id:'div2',
        fD:function(){
        document.title="hi"
    }
    });

    var oDiv3=new Drag();
    oDiv3.init({
        id:'div3',
        fD:function(){
        document.title='jerry'
    },
        fU:function(){
        document.title='byebye'
    }
    });
}
    function Drag(){
        this.oDiv=null;
        this.disX=0;
        this.disY=0;

        this.settings={
            fD:function(){},
            fU:function(){}
        }
    }

Drag.prototype.init=function(opt){
    var _this=this;
    
    
    extend(this.settings,opt);

    this.oDiv=document.getElementById(opt.id);
    this.oDiv.οnmοusedοwn=function(ev){
        var ev=ev || window.event;
        _this.fnDown(ev);
        _this.settings.fD();

        document.onmousemove=function(ev){
            var ev=ev || window.event;
            _this.fnMove(ev);
        }
        document.onmouseup=function(){
            _this.fnUp();
            _this.settings.fU();
        }

        return false;
    }
}
Drag.prototype.fnDown=function(ev){
    var ev=ev || window.event;
    this.disX=ev.clientX-this.oDiv.offsetLeft;
    this.disY=ev.clientY-this.oDiv.offsetTop;
}
Drag.prototype.fnMove=function(ev){
    this.oDiv.style.left=ev.clientX-this.disX+'px';
    this.oDiv.style.top=ev.clientY-this.disY+'px';
}
Drag.prototype.fnUp=function(){
    document.onmousedown=null;
    document.onmousemove=null;
}

function extend(obj1,obj2){
    for (var i in obj2){
        obj1[i]=obj2[i];
    }
}
</script>

 

转载于:https://www.cnblogs.com/JerryWang24/p/4537993.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值