SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Evangelist
    Join Date
    May 2006
    Posts
    457
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    drag and drop image

    Hello all, ive got the following code to drag and drop an image around the page.

    Is there any way of achiving the following
    - when the button is clicked, the possition will not be altered. i.e. the position of the image will be saved after the drag and drop has been done.
    - not allowing the image to go around the wole of the page, only allow it to drag and drop around say 1/2 of the page

    Any ideas?

    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
    <script runat="server">
    
        protected void Button1_Click(object sender, EventArgs e)
        {
    
        }
    </script>
    
    <html xmlns="http://www.w3.org/1999/xhtml" >
    <head runat="server">
        <title>Untitled Page</title>
        <style>
    <!--
    .dragme{position:relative;}
    -->
    </style>
    
    <script language="JavaScript1.2">
    var ie=document.all;
    var nn6=document.getElementById&&!document.all;
    
    var isdrag=false;
    var x,y;
    var dobj;
    
    function movemouse(e)
    {
      if (isdrag)
      {
        dobj.style.left = nn6 ? tx + e.clientX - x : tx + event.clientX - x;
        dobj.style.top  = nn6 ? ty + e.clientY - y : ty + event.clientY - y;
        return false;
      }
    }
    
    function selectmouse(e) 
    {
      var fobj       = nn6 ? e.target : event.srcElement;
      var topelement = nn6 ? "HTML" : "BODY";
    
      while (fobj.tagName != topelement && fobj.className != "dragme")
      {
        fobj = nn6 ? fobj.parentNode : fobj.parentElement;
      }
    
      if (fobj.className=="dragme")
      {
        isdrag = true;
        dobj = fobj;
        tx = parseInt(dobj.style.left+0);
        ty = parseInt(dobj.style.top+0);
        x = nn6 ? e.clientX : event.clientX;
        y = nn6 ? e.clientY : event.clientY;
        document.onmousemove=movemouse;
        return false;
      }
    }
    
    document.onmousedown=selectmouse;
    document.onmouseup=new Function("isdrag=false");
        </script>
    
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
            <img src="" class="dragme"/>
            <asp:Button ID="Button1" runat="server" Text="Button" />
        </div>
        </form>
    </body>
    </html>

  2. #2
    I'll take mine raw silver trophy MikeFoster's Avatar
    Join Date
    Dec 2002
    Location
    Alabama, USA
    Posts
    2,560
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I just added "bounding rectangle" support to my xEnableDrag function. Here's a demo.

    In the function _xOMM look at the following to see how I implemented it.
    Code:
        if (el.xREC) { 
          var r = el.xREC, x = xPageX(el) + dx, y = xPageY(el) + dy;
          var b = (x >= r.x1 && x+xWidth(el) <= r.x2 && y >= r.y1 && y+xHeight(el) <= r.y2);
        }


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •