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

Web Page Animation

9. Moving Vertically

clr gif

Moving vertically is no more difficult than moving horizontally. All we need to do is set a vertical end position and then amend the mover function to update the vertical position instead of the horizontal one. For example:

endPos = (pageHeight()-objHeight('myobj'))/2;

This sets the end position to be the centre of the window vertically.

function mover() {
if (yyy < endPos) {
yyy +=2; moveit(); setTimeout('mover()',50);}
}

This adds 2 to the Y coordinate to move our object down the screen each time instead of subtracting from the X coordinate to move the object to the left.

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

If we wanted to position the objects end position relative to the bottom edge of the screen we would use a fixed distance from posBottom() 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.