Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New Coder
    Join Date
    Oct 2008
    Posts
    10
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Making a div move to point A then point B (code isn't working).

    Here is the code I have right now. It won't let me move the div back to a certain point unless it's to the left, or beyond the div's specified 'left:' attribute. I can't seem to make it stop before it hits that point; the script just doesn't function if I try doing that.

    Code:
    var the_timeout;
    var direction = "right";
    
    function moveDiv()
    {
      var new_left;
    
      var the_style = getStyleObject("myDiv");
      if (the_style)
      {
    
        var current_left = parseInt(the_style.left);
        if (direction == "right")
        {
          new_left = current_left + 5;
        } 
        else 
        {
          new_left = current_left - 5;
        }
    
        if (new_left > 300)
        {
          direction = "left";
          window.document.bee.src = "bee_left.gif";
        }
    
        if (new_left < 30)
        {
          the_timeout = setTimeout('moveDiv();',10); return true;
        }
    
        if (document.layers) 
        {
          the_style.left = new_left;
        }
        else 
        {  
          the_style.left = new_left + "px";
        }
    
        the_timeout = setTimeout('moveDiv();',10);
      }
    }

  • #2
    Senior Coder
    Join Date
    Mar 2005
    Location
    Portsmouth UK
    Posts
    4,518
    Thanks
    3
    Thanked 506 Times in 493 Posts
    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    
    <head>
      <title></title>
    <script language="JavaScript" type="text/javascript">
    /*<![CDATA[*/
    var the_timeout;
    var direction = "right";
    
    function moveDiv(){
     var new_left;
     var obj = document.getElementById("myDiv");
     if (obj){
      var current_left = parseInt(obj.style.left);
      if (direction == "right"){
       new_left = current_left + 5;
      }
      else{
       new_left = current_left - 5;
      }
      if (new_left > 300){
       direction = "left";
    //   window.document.bee.src = "bee_left.gif";
      }
      if (new_left < 30){
       direction = "right";
      }
      obj.style.left = new_left + "px";
      the_timeout = setTimeout(function(){ moveDiv(); },10);
     }
    }
    /*]]>*/
    </script></head>
    
    <body onload="moveDiv();">
    <div id="myDiv" style="position:absolute;left:110px;top:10px;width:20px;height:20px;background-Color:red; " ></div>
    </body>
    
    </html>
    Vic

    God Loves You and will never love you less.

    http://www.vicsjavascripts.org/Home.htm

    If my post has been useful please donate to http://www.operationsmile.org.uk/


  •  

    Posting Permissions

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