Random Move 2

The object scrolls between a start position and an end position.
The script calculates the travel distance (End - Start) which is then divided by the StepNum value to give the StepSize.
StepSize is then the amount the object is moved each StepNum.
The end position is randomly generated for the object to scroll to, once reached, this end position then becomes the start position and a new end position is generated for the next scroll.
The speed of the effect is altered by pause and/or StepNum.
For this example the StepNum is randomly generated for each new direction, this can be set within the script to be a constant speed.

DistX = EndX - StartX
DistY = EndY - StartY

StepSizeX = DistX / StepNum
StepSizeY = DistY / StepNum

000
000

000
000
=
=

=
=

000
000

000
000
-
-

/
/

000
000

000
000
New Position X
New Position Y

StepCount
000
000

0

<script type="text/javascript">
<!-- active = 0; pause=100 // pause time between steps StepNum=50 // use for constant number of steps taken to move to new position function Init(EndX, EndY,id) { if(active==1){return} //StepNum=1+Math.round(Math.random()*49) // use for random steps on each run same_id=id StartX =parseInt(document.getElementById(id).style.left) StartY =parseInt(document.getElementById(id).style.top) distX = EndX - StartX distY = EndY - StartY StepSizeX = Math.round(distX / StepNum) StepSizeY = Math.round(distY / StepNum) StepCount=0 // reset to zero EndX2=EndX // for repeat function EndY2=EndY // for repeat function step(); } StepCount=0 timer="" function step() { active=1 document.all[same_id].style.pixelLeft = StartX += StepSizeX; document.all[same_id].style.pixelTop = StartY += StepSizeY; if (StepCount >= StepNum - 1) { clearInterval(timer); active = 0; repeat() // continuous run } else { StepCount++; timer=setTimeout("step()",pause) } } function repeat(){ // run continuously document.all.moveme1.style.pixelLeft=StartX // confirm objects last StartX position document.all.moveme1.style.pixelTop=StartY // confirm objects last StartY position EndX2=Math.floor(Math.random()*(document.body.clientWidth-50)) // new EndX position EndY2=Math.floor(Math.random()*(document.body.clientHeight-50)) // new EndY position if(StartX<0){StartX=0} if(StartY<0){StartY=0} Init(EndX2,EndY2,same_id) }
setTimeout("Init(Math.floor(Math.random() * (document.body.clientWidth-50)),Math.floor(Math.random() * (document.body.clientHeight-50)),'moveme1')",2000) // random initial start position
// -->
</script>

<DIV ID="moveme1" STYLE="position: absolute; left:10; top:10">Your Contents</DIV>