Forum Moderators: open

Message Too Old, No Replies

DHTML and animating objects

         

moonbather

2:32 am on Sep 18, 2003 (gmt 0)

10+ Year Member



Hello,

I've been experimenting with moving objects using DHTML, and the snippet below is one such experiment.


<head>
<script type="text/javascript" language="javascript">
objectLeftPos = 0
objectTopPos = 0;
function moveRight(item,endpo) {

if (objectLeftPos < endpo) {
objectLeftPos += 10;
document.getElementById(item).style.left = objectLeftPos;
setTimeout("moveRight('"+item+"','"+endpo+"')",80)
}
else window.status=document.getElementById("object1").offsetLeft+'px; '+document.getElementById("object2").offsetLeft+'px ;'+document.getElementById("object1").offsetLeft+'px'
// to reveal x-coord of end position of objects
}
</script></head>
<body>
<div id="object1" style="position:absolute;top:100px;left:0px;width:100px;">Object 1</div>
<div id="object2" style="position:absolute;top:150px;left:0px;width:100px;">Object 2</div>
<div id="object3" style="position:absolute;top:200px;left:0px;width:100px;">Object 3</div>
<form><input type="button" value="Move Right" onclick="moveRight('object1','300');moveRight('object2','300');moveRight('object3','300');">
</form>

I'm looking to have all three objects moving at the same time and ending up at the same x-coordinate, and wondered if anyone knew what the code "should" be to bring that about?

At the moment there is a horizontal difference of 10px between each object's end position (as revealed by the status bar), which is somewhat baffling.

HocusPocus

12:06 pm on Sep 18, 2003 (gmt 0)

10+ Year Member



objectLeftPos is shared between all 3 objects. You're adding 10 to objectLeftPos at each call of moveRight().

This'll do the trick-

curObj=document.getElementById(item);

if (curObj.offsetLeft < endpo)
{
curObj.style.left = curObj.offsetLeft + 10;
setTimeout("moveRight('"+item+"','"+endpo+"')",80)
....

moonbather

7:10 pm on Sep 18, 2003 (gmt 0)

10+ Year Member



HocusPocus, it did the trick. Thanks a lot.