Page 1 of 2 12 LastLast
Results 1 to 10 of 12

Thread: Modifying drag script for drag limit: howto??

  1. #1
    Join Date
    Aug 2005
    Posts
    971
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Modifying drag script for drag limit: howto??

    Hello all,
    I was playing around this:

    Code:
    <html>
    <head>
    <script type="text/javascript">
    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,10);
        ty = parseInt(dobj.style.top+0,10);
        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>
    <style type="text/css">
    .dragme{position:relative;}
    #test {
    width: 100px;
    height: 100px;
    background-color: red;
    }
    </style>
    </head>
    <body>
    <div id="test" class="dragme"></div>
    </body>
    </html>
    drag script and then I thought of something. How do I limit the drag, for example how do I stop the element being dragged from dragging if the element reaches 100px far from the left?? I tried :
    Code:
    if(!dobj.offsetLeft > 100){
    dobj.style.left = nn6 ? tx + e.clientX - x : tx + event.clientX - x;
    dobj.style.top = nn6 ? ty + e.clientY - y : ty + event.clientY - y;
    } else {
    return false;
    }
    but it doesn't seem to work. Any help here PLEASE!!!!

    Thanks.

  2. #2
    Join Date
    Jul 2006
    Location
    Canada
    Posts
    2,581
    Thanks
    13
    Thanked 28 Times in 28 Posts

    Default

    Set the onmousemove event handler to null, instead of trying to set the width when the user drags past a certain point.
    - Mike

  3. #3
    Join Date
    Aug 2005
    Posts
    971
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by mburt
    instead of trying to set the width when the user drags past a certain point.
    What do you mean??

  4. #4
    Join Date
    Jul 2006
    Location
    Canada
    Posts
    2,581
    Thanks
    13
    Thanked 28 Times in 28 Posts

    Default

    I meant to say (in place of width): the pixels from the left of the screen

    Sorry about that.
    - Mike

  5. #5
    Join Date
    Aug 2005
    Posts
    971
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    mburt: never mind about that but I still can't get what you are trying to say. If I set the onmousemove to null then the drag will stop completely. Here's my code so far(if I understood what you meant).

    Code:
    <html>
    <head>
    <script type="text/javascript">
    var ie=document.all;
    var nn6=document.getElementById&&!document.all;
    var isdrag=false;
    var x,y;
    var dobj;
    
    function movemouse(e){
      if (isdrag){
      if(dobj.offsetLeft < 500){
        dobj.style.left = nn6 ? tx + e.clientX - x : tx + event.clientX - x;
    //    dobj.style.top  = nn6 ? ty + e.clientY - y : ty + event.clientY - y;
    } else {
    document.onmousemove = null;
    }
        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,10);
        ty = parseInt(dobj.style.top+0,10);
        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>
    <style type="text/css">
    .dragme{position:relative;}
    #test {
    width: 100px;
    height: 100px;
    background-color: red;
    }
    </style>
    </head>
    <body>
    <div id="test" class="dragme"></div>
    </body>
    </html>

  6. #6
    Join Date
    Jul 2006
    Location
    Canada
    Posts
    2,581
    Thanks
    13
    Thanked 28 Times in 28 Posts

    Default

    Yes. You want it to stop completely when the value is more or less then the desired value, then you set the "else" statement to:
    Code:
    document.onmousemove = movemouse;
    - Mike

  7. #7
    Join Date
    Aug 2005
    Posts
    971
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    mburt: actually that's not what I meant. I need it to stop completely *but* it must be abled to be dragged again. If I set it to document.onmousemove = null it can't be dragged once it exceedes it's limit. Well, let me try what you suggested, here's my code:

    Code:
    <html>
    <head>
    <script type="text/javascript">
    var ie=document.all;
    var nn6=document.getElementById&&!document.all;
    var isdrag=false;
    var x,y;
    var dobj;
    
    function movemouse(e){
      if (isdrag){
      if(dobj.offsetLeft < 500){
        dobj.style.left = nn6 ? tx + e.clientX - x : tx + event.clientX - x;
    //    dobj.style.top  = nn6 ? ty + e.clientY - y : ty + event.clientY - y;
    } else {
    document.onmousemove = mousemove;
    }
        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,10);
        ty = parseInt(dobj.style.top+0,10);
        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>
    <style type="text/css">
    .dragme{position:relative;}
    #test {
    width: 100px;
    height: 100px;
    background-color: red;
    }
    </style>
    </head>
    <body>
    <div id="test" class="dragme"></div>
    </body>
    </html>
    Gives out an error: Error: mousemove is not defined
    Source File: ----:///----/------/testdrag.html
    Line: 16

  8. #8
    Join Date
    Jul 2006
    Location
    Canada
    Posts
    2,581
    Thanks
    13
    Thanked 28 Times in 28 Posts

    Default

    I meant to say:
    Code:
    document.onmousemove = movemouse
    The point is to use whatever your function is for actually moving the object and calling it again.
    - Mike

  9. #9
    Join Date
    Jul 2006
    Location
    Canada
    Posts
    2,581
    Thanks
    13
    Thanked 28 Times in 28 Posts

    Default

    I see your point though. I'm not really sure why you wouldn't be able to continue dragging, I'll try to work it out.
    - Mike

  10. #10
    Join Date
    Aug 2005
    Posts
    971
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    mburt: did you even bother to read the initial code?? The function to move the object *is* movemouse, I apologize if it was a little harsh.

    EDIT: You've posted before me.

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
  •