var drag={ "obj":null, "init":function(handle, dragbody, e){ if (e == null) { handle.onmousedown=drag.start; } handle.root = dragbody; if(isnan(parseint(handle.root.style.left)))handle.root.style.left="0px"; if(isnan(parseint(handle.root.style.top)))handle.root.style.top="0px"; handle.root.ondragstart=new function(); handle.root.ondragend=new function(); handle.root.ondrag=new function(); if (e !=null) { var handle=drag.obj=handle; e=drag.fixe(e); var top=parseint(handle.root.style.top); var left=parseint(handle.root.style.left); handle.root.ondragstart(left,top,e.pagex,e.pagey); handle.lastmousex=e.pagex; handle.lastmousey=e.pagey; document.onmousemove=drag.drag; document.onmouseup=drag.end; } }, "start":function(e){ var handle=drag.obj=this; e=drag.fixevent(e); var top=parseint(handle.root.style.top); var left=parseint(handle.root.style.left); //alert(left) handle.root.ondragstart(left,top,e.pagex,e.pagey); handle.lastmousex=e.pagex; handle.lastmousey=e.pagey; document.onmousemove=drag.drag; document.onmouseup=drag.end; return false; }, "drag":function(e){ e=drag.fixevent(e); var handle=drag.obj; var mousey=e.pagey; var mousex=e.pagex; var top=parseint(handle.root.style.top); var left=parseint(handle.root.style.left); if(document.all){drag.obj.setcapture();}else{e.preventdefault();};//作用是将所有鼠标事件捕获到handle对象,对于firefox,以用preventdefault来取消事件的默认动作: var currentleft,currenttop; currentleft=left+mousex-handle.lastmousex; currenttop=top+(mousey-handle.lastmousey); handle.root.style.left=currentleft +"px"; handle.root.style.top=currenttop+"px"; handle.lastmousex=mousex; handle.lastmousey=mousey; handle.root.ondrag(currentleft,currenttop,e.pagex,e.pagey); return false; }, "end":function(){ if(document.all){drag.obj.releasecapture();};//取消所有鼠标事件捕获到handle对象 document.onmousemove=null; document.onmouseup=null; drag.obj.root.ondragend(parseint(drag.obj.root.style.left),parseint(drag.obj.root.style.top)); drag.obj=null; }, "fixevent":function(e){//格式化事件参数对象 var sl = math.max(document.documentelement.scrollleft, document.body.scrollleft); var st = math.max(document.documentelement.scrolltop, document.body.scrolltop); if(typeof e=="undefined")e=window.event; if(typeof e.layerx=="undefined")e.layerx=e.offsetx; if(typeof e.layery=="undefined")e.layery=e.offsety; if(typeof e.pagex == "undefined")e.pagex = e.clientx + sl - document.body.clientleft; if(typeof e.pagey == "undefined")e.pagey = e.clienty + st - document.body.clienttop; return e; } };