So You Want to Move A Layer, Huh?

By Joe Burns

http://www.htmlgoodies.com/beyond/dhtml/article.php/3470441/So-You-Want-to-Move-A-Layer-Huh.htm (Back to article)

...use these to jump around or read it all
[Moving the Layer] [The Code]
[Deconstructing The Script]
[Moving the Layer From Off Screen]

     First Things First: We're dealing with Layers here so you'll need to be running a Netscape Navigator browser version 4.0 or better. If not...error.

     Every now and again I get an email asking how people get things to fly around their screen. When I am given an URL to look at, I find that a good bit of the time, it's a layer set up to follow a path across the browser screen. It's actually easy enough to do.

      Here I'll show you two things:

  1. The first is a basic JavaScript that sets a path for a Layer to follow. The script is very easy to play with to get all kinds of different paths.
  2. The second is the same script using negative numbers to have the layer roll in from off of the screen. On that layer will be a link that controls what shows up on the screen. Very neat.


Moving the Layer

     OK, here's a simple script that moves the Layer. You can set the path of the layer, the speed of the movement, the size of each movement, and where the layer stops, if you want it to stop at all. I have it set up to stop.

See the Moving Layer


The Code

     Here you go...

<SCRIPT LANGUAGE="javascript">

x=1
y=1

function moveIt()
{
x=x+2
y=y+2

document.layers[0].moveToAbsolute(x,y)

if (x >= 100)
     {document.layers[0].stop}
else
     {setTimeout("move()",5)}
}

</SCRIPT>

<BODY onLoad="moveIt()">

<LAYER NAME="bigLayer" BGCOLOR="ff00ff" HEIGHT="100" WIDTH="100" LEFT=1 TOP=1>Hey!</LAYER>



Deconstructing the Script

     The easiest way to roll through this script is to start with the Layer first. Once you have that, we can start moving it around. The code looks like this:

<LAYER NAME="bigLayer" BGCOLOR="ff00ff" HEIGHT="100" WIDTH="100" LEFT=1 TOP=1>Hey!</LAYER>

     If the code is foreign to you, try reading my first Layer tutorial before you get into this one. It'll make your life a little easier.

     And so we start...

The Script

<SCRIPT LANGUAGE="javascript">

     This should look familiar. That starts off every JavaScript, and this is a JavaScript.

x=1
y=1



     Remember I told you about this in the section just above? These are the starting coordinates of the layer's upper left hand corner. These numbers should be the same as the numbers set in the Layer itself. If they're not, then you're going to get a bit of a jump before the script kicks in.

     The layer will first appear where LEFT and TOP settings place it, then jump to where these x and y coordinates say it should be. See the problem? Get the numbers equal and you'll get a nice smooth start. If you want to see what I mean, try setting the numbers to different values and running the script. You can always change it back later.

function moveIt()
{
x=x+2
y=y+2


     Here's where you control the movement of the Layer. The coordinate "x" denotes the Layer's movement to the right. The "y" coordinate controls the Layer's movement downwards.

     Right now I have it set to move in a perfect 45 degree line down across the page. For every two pixels to the right, the block also moves two pixels down. That happens again and again and the Layer moves down the line.

     You could also try:

     you might even try setting up a small math equation to create random numbers to give a really weird look.

document.layers[0].moveToAbsolute(x,y)

     Here's the magic. This line actually moves the layer to the next point set up by the adding of numbers to x and y.

if (x >= 100)
     {document.layers[0].stop}
else
     {setTimeout("move()",5)}

     This is a conditional statement that allows you to stop the layer at a certain point.

     The first line states that if x becomes greater than or equal to 100, then something should happen. Now, I chose 100 out of the clear blue sky. You can set it to any thing you want. The higher the number, the longer the Layer will move. How quickly you get to the number also depends on how quickly "x" adds up. Remember that earlier we set up "x" to have 2 added each time the script rolls through. That means after the script ran 49 times (it wouldn't have run the 50th time), the script stops and the Layers stops. Get the concept? The higher the number, the longer the script will take to reach it and the longer the script will run moving the Layer across the screen.

     One more thing. I have this line set up to read as "greater than or equal to" because I found out the hard way that setting this line so that "x" has to equal a specific number is bad. I first had it so that "x" was suppose to equal 100 to stop. The problem was that I has it so that only one was added each time. That means x was 99 and then 101. It never did get to equal 100. Oops. Stick with the "greater than or equal to" format.

     When the script is running, if "x" has not yet reached at least 100, then the function rolls through again thanks to the setTimeout() command. See the "5" after the comma? That is a measure in 1/1000ths of a second of how long the script should wait to run again. Higher numbers scroll slower. This Layer moves pretty quickly because it gets bumped up every 5/1000ths of a second.

}

</SCRIPT>

     Finally the curly bracket rounds out the function and the script comes to an end.

     The script is triggered to run through the use of an onLoad event Handler in the BODY command: onLoad="moveIt()".


Moving the Layer From Off Screen

     This is becoming a popular effect. In fact, this is being asked for more often than just the simple moving layer. I'm using the same script as above with just a few simple changes. I also added a link

See it in Action

     OK, that's pretty smooth, you have to admit. Before I show you the code, let's think it through.

     That should do it, and it does. Here's the script:

<SCRIPT LANGUAGE="javascript">

x=-100
y=200

function move()
{
x=x+1
y=y+0

document.layers[0].moveToAbsolute(x,y)

if (x == -1)
{document.layers[0].stop}
else
{setTimeout("move()",25)}

}


</SCRIPT>

     The link on the Layer is just a normal hypertext link except I put TARGET="main" in the code. That's it.

     The script is triggered again by an onLoad Event Handler in the BODY tag. Great effect.


That's That

     This is a very basic script. It has limited movement. Play with it. See if you can get it to do different things. If you get a new and strange layer movement, send it to me. Maybe I'll post it here - who knows?

Enjoy!

[Moving the Layer] [The Code]
[Deconstructing The Script]
[Moving the Layer From Off Screen]