|
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <script type="text/javascript"> var drag=(function(){ var isdown,div,x,y,div2; var init=function(id,id2){ div=document.getElementById(id); div2=document.getElementById(id2); div.onmousedown=function(e){ e=e||window.event; isdown=true; div.style.cursor="move"; // x=e.offsetX||e.layerX; // y=e.offsetY||e.layerY; if(document.all){ x=e.offsetX;y=e.offsetY }else{ x=e.layerX;y=e.layerY; } return false; } document.onmouseup=function(){ isdown=false; div.style.cursor="default"; return false; } document.onmousemove=function(e){ e=e||window.event; if(isdown){ div2.style.left=e.clientX-x+"px"; div2.style.top=e.clientY-y+"px"; return false; } } } return init; })(); window.onload=function(){ drag("drag","drag2"); } </script> </head> <body> <div id="drag2" style="width: 200px;height: 200px;position: absolute;background: green;z-index: 2;"> <div id="drag" style="width: 200px;height: 20px;position: absolute;background: blue;z-index: 2;"></div> </div> </body> </html> |