<html>
<head>
</head>


<script  language="javascript"  >


 /*Click and drag to follow the div */


 var div_a1;


 function init(){
   div_a1 = document.getElementById('a1');
   var groundDiv = document.getElementById('groundDiv');
   groundDiv.addEventListener('mousedown', mouseDown);
   groundDiv.addEventListener('mousemove', mouseMove);
   groundDiv.addEventListener('mouseup', mouseUp);
 }


 var isClick = false; 
 function mouseDown(e){
   isClick = true;
 }
 
 function mouseMove(e){
   if( isClick == true ){
     div_a1.style.left = e.offsetX - 20 + 'px';
     div_a1.style.top = e.offsetY - 20 + 'px';     
   }
 }
 
 function mouseUp(e){
   isClick = false;
 }


</script>
    <body  onload="init();" >
        <div id="groundDiv"  style="position:relative; width:99%; height:99%; border:0x solid black;"></div>
        <div id='a1'  style="position:absolute; width:3px; height:3px; border:2px solid black;top:2px;left:3px  " ></div>
    </body>
</html>




 

 

Posted by 혜화초보
,