实例代码一见: 复制代码 代码如下: <BR>#main div{position:absolute;width:220px;height:150px;border:1px solid #999;} <BR> <BR>var a; <BR>document.onmouseup = function() { <BR>if (!a) return; <BR>a = ""; <BR>}; <BR>document.onmousemove = function(d) { <BR>if (!a) return; <BR>d=d||event; <BR>a.style.left = (d.clientX - b) + "px"; <BR>a.style.top = (d.clientY - c) + "px"; <BR>}; <BR>function $(o, e) { <BR>a = o; <BR>b = e.clientX - parseInt(a.style.left); <BR>c = e.clientY - parseInt(a.style.top); <BR>} <BR> 1 2 3 4 5 6 效果: #main div{position:absolute;width:220px;height:150px;border:1px solid #999;} var a; document.onmouseup = function() { if (!a) return; a = ""; }; document.onmousemove = function(d) { if (!a) return; d=d||event; a.style.left = (d.clientX - b) + "px"; a.style.top = (d.clientY - c) + "px"; }; function $(o, e) { a = o; b = e.clientX - parseInt(a.style.left); c = e.clientY - parseInt(a.style.top); } 1 2 3 4 5 6 [Ctrl+A 全选 注:如需引入外部Js需刷新才能执行] 实例代码二见: 复制代码 代码如下: 无标题文档 <BR>#test{width:200px; height:200px; background:pink; cursor:move; position:absolute; left:100px; top:100px} <BR> <BR>var obj=document.getElementById("test"); <BR>var b; <BR>obj.onmousedown=function(e){ <BR>b=true; <BR>var divLeft=parseFloat(window.getComputedStyle?window.getComputedStyle(obj,null).left:obj.currentStyle.left); <BR>var divTop=parseFloat(window.getComputedStyle?window.getComputedStyle(obj,null).top:obj.currentStyle.top); <BR>var e=e||event; <BR>var divX=e.clientX-divLeft; //计算鼠标和div边框的距离 <BR>var divY=e.clientY-divTop; <BR>document.onmousemove=function(e){ <BR>if(b){ <BR>var e=e||event; //兼容IE8及以下 <BR>obj.style.left=e.clientX-divX+"px"; <BR>obj.style.top=e.clientY-divY+"px"; <BR>} <BR>} <BR>} <BR>document.onmouseup=function(){ <BR>b=false; <BR>} <BR> 效果: 无标题文档 #test{width:200px; height:200px; background:pink; cursor:move; position:absolute; left:100px; top:100px} var obj=document.getElementById("test"); var b; obj.onmousedown=function(e){ b=true; var divLeft=parseFloat(window.getComputedStyle?window.getComputedStyle(obj,null).left:obj.currentStyle.left); var divTop=parseFloat(window.getComputedStyle?window.getComputedStyle(obj,null).top:obj.currentStyle.top); var e=e||event; var divX=e.clientX-divLeft; //计算鼠标和div边框的距离 var divY=e.clientY-divTop; document.onmousemove=function(e){ if(b){ var e=e||event; //兼容IE8及以下 obj.style.left=e.clientX-divX+"px"; obj.style.top=e.clientY-divY+"px"; } } } document.onmouseup=function(){ b=false; } [Ctrl+A 全选 注:如需引入外部Js需刷新才能执行] 实例代码三见(拖动把柄): 无标题文档 *{margin:0;padding:0} #testBox{ width:300px; height:300px; background:red; position:absolute; left:0; top:0} #test{width:200px; height:200px; background:pink; cursor:move; margin:30px} 在这里才能移动 var obj=document.getElementById("test"); var objBox=document.getElementById("testBox"); var b; obj.onmousedown=function(e){ b=true; var divLeft=parseFloat(window.getComputedStyle?window.getComputedStyle(objBox,null).left:objBox.currentStyle.left); var divTop=parseFloat(window.getComputedStyle?window.getComputedStyle(objBox,null).top:objBox.currentStyle.top); var e=e||event; var divX=e.clientX-divLeft; //计算鼠标和div边框的距离 var divY=e.clientY-divTop; document.onmousemove=function(e){ if(b){ var e=e||event; //IE8及以下浏览器得写这句 objBox.style.left=e.clientX-divX+"px"; objBox.style.top=e.clientY-divY+"px"; } } } document.onmouseup=function(){ b=false; } [Ctrl+A 全选 注:如需引入外部Js需刷新才能执行]