JS自定义拖拽setCapture

window.onload = function(){
        
        var oDiv = document.getElementById("div1");   //获取DIV
        
        oDiv.onmousedown = function(ev){
                
                var oEvent = ev || event;     //IE 或 FF CH SA OP
                var oBox = document.createElement("div");  //创建一个DIV
                oBox.className = "box";    //给创建的DIV添加名为box的class
                document.body.appendChild(oBox);   //添加到body
                oBox.style.width = this.offsetWidth - 2 + "px";    // 设置新创建的DIV的宽度
                oBox.style.height = this.offsetHeight - 2 + "px";  // 设置新创建的DIV的高度
                oBox.style.left = this.offsetLeft + "px";   // 设置新创建的DIV的左边移
                oBox.style.top = this.offsetTop + "px";     // 设置新创建的DIV的上边移
                
                mouX = oEvent.clientX - oDiv.offsetLeft;
                mouY = oEvent.clientY - oDiv.offsetTop;
                
                if(oDiv.setCapture){
                        this.onmousemove = mouseMove;
                        this.onmouseup = mouseUp;
                        this.setCapture();  //当前的DIV捕获事件,只有在IE中有效
                }else{
                        document.onmousemove = mouseMove;
                        document.onmouseup = mouseUp;
                };
                function mouseMove(ev){
                        var oEvent = ev || event;
                        var l = oEvent.clientX - mouX;
                        var t = oEvent.clientY - mouY;
                        if(l < 0){
                                l = 0;
                        }else if( l > document.documentElement.clientWidth - oDiv.offsetWidth){
                                l = document.documentElement.clientWidth - oDiv.offsetWidth;
                        }
                        
                        if(t < 0){
                                t = 0;
                        }else if(t > document.documentElement.clientHeight - oDiv.offsetHeight){
                                t = document.documentElement.clientHeight - oDiv.offsetHeight;
                        }
                        
                        
                        oBox.style.left = l + "px";
                        oBox.style.top = t + "px";
                        
                }
                function mouseUp(){
                        oDiv.style.left = oBox.style.left;
                        oDiv.style.top = oBox.style.top;
                        document.body.removeChild(oBox);
                        this.onmousemove = null;
                        this.onmouseup = null;
                        if(this.releaseCapture){
                                this.releaseCapture();
                        }
                }
                return false;
        };
        
};

我的小站:爆笑家园
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值