<div class="codetitle"> <span><a style="CURSOR: pointer" data="2912" class="copybut" id="copybut2912" onclick="doCopy('code2912')"><u>复制代码</u></a></span> 代码如下:</div> <div class="codebody" id="code2912"> <br><script type="text/javascript"> <BR><!-- <BR>window.onload=function(){ <BR>objDiv = document.getElementById('drag'); <BR>drag(objDiv); <BR>}; <BR>function drag(dv){ <BR>dv.onmousedown=function(e){ <BR>var d=document; <BR>e = e || window.event; <BR>var x= e.layerX || e.offsetX; <BR>var y= e.layerY || e.offsetY; <BR>//设置捕获范围 <BR>if(dv.setCapture){ <BR>dv.setCapture(); <BR>}else if(window.captureEvents){ <BR>window.captureEvents(Event.MOUSEMOVE | Event.MOUSEUP); <BR>} <BR>d.onmousemove=function(e){ <BR>e= e || window.event; <BR>if(!e.pageX)e.pageX=e.clientX; <BR>if(!e.pageY)e.pageY=e.clientY; <BR>var tx=e.pageX-x; <BR>var ty=e.pageY-y; <BR>dv.style.left=tx; <BR>dv.style.top=ty; <BR>}; <BR>d.onmouseup=function(){ <BR>//取消捕获范围 <BR>if(dv.releaseCapture){ <BR>dv.releaseCapture(); <BR>}else if(window.captureEvents){ <BR>window.captureEvents(Event.MOUSEMOVE|Event.MOUSEUP); <BR>} <BR>//清除事件 <BR>d.onmousemove=null; <BR>d.onmouseup=null; <BR>}; <BR>}; <BR>} <BR>//--> <BR></script> <br><div id="drag" style="position:absolute;left:12px;top:24px;width:100;height:150;border:1px solid #000000;z-index:1;background:#eeeeee">drag me</div> <br> </div> <br>setCapture 的意思就是设置一个对象的方法被触发的范围,或者作用域。 <br>如果不设置,则div只在当前窗口内被触发。如果设置,则在整个浏览器范围内被触发,也就是可以拖到浏览器外面