自作拖拽事件

varbox=document.getElementById("box");
    box.style.position="absolute";
    box.style.width="160px";
    box.style.height="120px";
    box.style.background="red";
    box.style.left="0px";
    box.style.top="0px";
    //标准化事件
    var mx,my,ox,oy,o;
    function e(event){
            if(!event){
                     event=window.event;
                     event.target=event.srcElement;
                     event.layerX=event.offsetX;
                     event.layerY=event.offsetY;
            }
            event.mx=event.pageX||event.clientX+document.body.scrollLeft;
            event.my=event.pageY||event.clientY+document.body.scrollTop;
            return event;
    }
    document.onmousedown=function(event){
            event=e(event);
            o=event.target;
            ox=parseInt(o.offsetLeft);
            oy=parseInt(o.offsetTop);
            mx=event.mx;
            my=event.my;
            document.onmousemove=move;
            document.onmouseup=stop;
 
    }
    function move(event){
            event=e(event);
            o.style.left=ox+event.mx-mx+"px";
            o.style.top=oy+event.my-my+"px";
    }
    function stop(event){
            event=e(event);
            ox=parseInt(o.offsetLeft);
            oy=parseInt(o.offsetTop);
            mx=event.mx;
            my=event.my;
            o=document.ommousemove=document.onmouseup=null;
    }


posted @ 2016-01-30 09:42  blfshiye  阅读(132)  评论(0编辑  收藏  举报