实现对div的拖拽
js
window.οnlοad=function(){ var oDiv1=document.getElementById('div1'); var oDiv2=document.getElementById('div2'); drag(oDiv1); drag(oDiv2); function drag(obj) { obj.οnmοusedοwn=function(ev){ var oEvent=ev||event; var disX=oEvent.clientX-obj.offsetLeft; var disY=oEvent.clientY-obj.offsetTop; obj.οnmοusemοve=function(ev){ var oEvent=ev||event; var left=oEvent.clientX-disX; var top=oEvent.clientY-disY; obj.style.left=left+'px'; obj.style.top=top+'px'; }; obj.οnmοuseup=function(){ obj.οnmοusemοve=null; obj.οnmοuseup=null; obj.releaseCapture&&obj.releaseCapture(); }; obj.setCapture&&obj.setCapture(); return false; }; }};
css
*{margin:0px;padding:0px;}#div1{width:200px;height:200px;background:red;position:absolute;left:100px;top:100px;}#div2{width:200px;height:200px;background:green;position:absolute;left:400px;top:100px;}
html