1. Home
  2. Computing & Technology
  3. JavaScript
This is a web page object.
move

Web Page Animation

8. Select End Position

clr gif

In the last tutorial we saw how we could easily move our object by a set distance (100 pixels in the example). To be able to move the object to a set position on the screen regardless of how far away that is just requires that we calculate the end position in a different way. Instead of the start position (xxx) less the distance to move was can instead specify the end position relative to the width of the screen. For example:

endPos = (pageWidth()-objWidth('myobj'))/2;

This sets the end position to be the centre of the window (at least horizontally). We need to place the left hand edge of the object half the width of the object to the left of a position half way across the screen so that the object will be exactly centred.

All of the other code for the animation would remain unchanged since we are only redefining the end position.

If we wanted to position the objects end position relative to the left hand edge of the screen we would use a fixed distance from posLeft() as our end position.

Explore JavaScript
About.com Special Features

Holiday Central

What to eat, where to go, fun things to do and how to save money on the perfect gifts. More >

Family Tech Center

Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. More >

  1. Home
  2. Computing & Technology
  3. JavaScript

©2009 About.com, a part of The New York Times Company.

All rights reserved.