把拖动的图片的相对于浏览器的x,y分别减去相对的那个图片相对于浏览器的x1,y1不就是吗

解决方案 »

  1.   

    net_lover(孟子E章) ,我对javascript不熟,你能不能把完整代码帖出来?
    麻烦你了!
      

  2.   

    自己查 MSDN 中对象的 offsetLeft/offsetTop 的用法
      

  3.   

    <html>
    <head>
    </head>
    <body>
    <img src="img/img2.gif" name="img2" width="468" height="205" id="img2">
    <div id="Layer1" style="position:absolute; z-index:3" ondragstart="setCapture();xx=event.x-this.offsetLeft;yy=event.y-this.offsetTop;"    
    ondrag='this.style.left=event.x-xx;this.style.top=event.y-yy'    
    ondragend="releaseCapture();gogogo(this.style.left,this.style.top)">
    <img id=img1> 
    </div>
    <div id="Layer2" style="position:absolute; width:248px; height:104px; z-index:2; left: 60px; top: 229px;"> 
      <p>src: 
        <input name="imgsrc" type="text" id="xy" value="img/img2.gif">
      </p>
      <p>x:
        <input name="x2" type="text" id="x2">
      </p>
      <p>y:
        <input name="y2" type="text" id="y2">
      </p>
    </div>
    <script>
    img1.src=document.all.imgsrc.value;
    var x1=img2.offsetLeft;
    var y1=img2.offsetTop;
    function gogogo(xx,yy){
    //var t=xx.parseInt(xx);
    document.all.x2.value=parseInt(xx)-x1;
    document.all.y2.value=parseInt(yy)-y1;
    }
    </script>
    </body>
    </html>
      

  4.   

    <html>
    <head>
    </head>
    <body>
    <img src="img/img2.gif" name="img2" width="468" height="205" id="img2">
    <div id=Layer1 style='position:absolute; left: 498px; top: 12px;' onmousedown="MouseDown(this)" onmousemove="MouseMove()" onmouseup="MouseUp();gogogo(this.style.left,this.style.top);">
    <img id=img1> 
    </div>
    <div id="Layer2" style="position:absolute; width:248px; height:104px; z-index:2; left: 60px; top: 229px;"> 
      <p>src: 
        <input name="imgsrc" type="text" id="xy" value="img/img2.gif">
      </p>
      <p>x:
        <input name="x2" type="text" id="x2">
      </p>
      <p>y:
        <input name="y2" type="text" id="y2">
      </p>
    </div>
    <p>
    <script>
    img1.src=document.all.imgsrc.value;
    var Obj
    function MouseDown(obj){
    Obj=obj
    Obj.setCapture()
    Obj.l=event.x-Obj.style.pixelLeft
    Obj.t=event.y-Obj.style.pixelTop
    Obj.style.cursor='crosshair'
    }
    function MouseMove(){
    if(Obj!=null){
    Obj.style.left = event.x-Obj.l
    Obj.style.top = event.y-Obj.t
    }
    }
    function MouseUp(){
    if(Obj!=null){
    Obj.releaseCapture()
    Obj.style.cursor='default'
    Obj=null
    }
    }var x1=img2.offsetLeft;
    var y1=img2.offsetTop;
    function gogogo(xx,yy){
    //var t=xx.parseInt(xx);
    document.all.x2.value=parseInt(xx)-x1;
    document.all.y2.value=parseInt(yy)-y1;
    }
    </script>
    </body>
    </html>