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

Thread: MoveBy, MoveTo

  1. #1
    TNO
    TNO is offline
    Regular Coder
    Join Date
    Apr 2005
    Posts
    213
    Thanks
    2
    Thanked 1 Time in 1 Post

    MoveBy, MoveTo

    I am testing a couple functions but am having trouble with one:

    Code:
    <html>
    <head>
       <script src="js/animation.js" type="text/javascript" defer></script>
    </head>
    <body>
       <span style="background-color:blue;position:absolute;top:10;left:10" id="Actor">Actor</span>
       <div style="position:absolute;top:80%;left:1%;background-color:gray;width:100%;font-weight:bold;">
          <hr>
             Move By:<input type="text" size="3" id="MBX" value="X" onclick="this.value=''">
             &nbsp<input size="3" type="text" value="Y" id="MBY" onclick="this.value=''">
             &nbsp&nbsp<button onclick="MoveBy(document.getElementById('Actor'),document.getElementById('MBX').value,document.getElementById('MBY').value)">Go</button>
          <br>
             Move To:<input type="text" size="3" id="MTX" value="X" onclick="this.value=''">
             &nbsp<input size="3" type="text" value="Y" id="MTY" onclick="this.value=''">
             &nbsp&nbsp<button onclick="MoveTo(document.getElementById('Actor'),document.getElementById('MTX').value,document.getElementById('MTY').value)">Go</button>
          <hr>
       </div>
    </body>
    </html>
    the .js:

    Code:
    function MoveBy(obj,x,y)
    {
    	if (parseInt(navigator.appVersion) >= 5 || navigator.appVersion.indexOf["MSIE 5"] != -1) 
    	{
    		obj.style.left = x + obj.style.left;
    		obj.style.top = y + obj.style.top;
    	}
    }
    
    function MoveTo(obj,x,y)
    {
    	if (parseInt(navigator.appVersion) >= 5 || navigator.appVersion.indexOf["MSIE 5"] != -1) 
    	{
    		obj.style.position = "absolute";
    		var units = (MoveTo.arguments.length > 3) ? MoveTo.arguments[3] : "px";
    		obj.style.left = x + units; 
    		obj.style.top = y + units; 
    	}
    }
    There must be something wrong with my logic. MoveTo works properly, but MoveBy seems to grow exponentially each time.

  • #2
    TNO
    TNO is offline
    Regular Coder
    Join Date
    Apr 2005
    Posts
    213
    Thanks
    2
    Thanked 1 Time in 1 Post
    Nevermind.....

    the function was treating the values as strings:

    X + obj.style.left

    parseInt(X) + parseInt(obj.style.left)


  •  

    Posting Permissions

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