纯js拖拽参考

function myDrag(obj){
    obj.onmousedown=function(e){
        var e=e||window.event;
        var diffX=e.clientX-this.offsetLeft;
        var diffY=e.clientY-this.offsetTop;
        document.onmousemove=function(e){
            var e=e||window.event;
            var left=e.clientX-diffX;
            var top=e.clientY-diffY;
            var leftMax=document.documentElement.clientWidth-obj.offsetWidth;
            var topMax=document.documentElement.clientHeight-obj.offsetHeight;
            if(left<0){
                left=0;
            }else if(left>leftMax){
                left=leftMax;
            }
            if(top<-20){
                 top=-20;
            }else if(top>topMax){
                top=topMax;
            }
            obj.style.left=left+"px";
            obj.style.top=top+"px";
        }
        document.onmouseup=function(){
        document.onmousemove=null;
        }
    }
}

 

posted @ 2014-09-11 09:13  H&K  阅读(220)  评论(0编辑  收藏  举报