Powered by Atomz.com

DHTML
-------------------------
Print | Fax
this article for free. 
-------------------------

Overview

Lesson 1

Lesson 2

Lesson 3
1  Let's Look at Scripting
2 Syntax: Microsoft vs. Netscape
3 Make It Work in Both Browsers
4 How the Script Works
5 A Trick for Accessing Objects
6 Make the Monkeys Chase One Another

Lesson 4

Lesson 5



Taylor's Dynamic HTML Tutorial
Lesson 3

by Taylor

Page 6 — Make the Monkeys Chase One Another

Now, you can control properties other than left and top, of course, like z-index. Let's say we wanted to have the monkeys chase one another around a desk.

You can almost hear the cries of, "Taylor, finish your tutorial!"
"NEVER!",
"No, come on, I have to edit it. Finish up!"
"NO!"
"I need at least two weeks to produce it!"
"STOP BUGGIN' ME, I'M PLAYING LEGOS!!!"

Besides modifying the z-index of each monkey, I've also used one JavaScript routine to move all the monkeys (rather than having a routine for each monkey).

    function moveMonkey(monkey, dir) {
    
      if (document.all) {
    
        var wtMonkey = document.all(monkey).style;
    
      } else if (document.layers) {
    
        var wtMonkey = document.monkeyContainer.document.layers[monkey];
    
      }
    
    
    
      if ((parseInt(wtMonkey.left) <0) || (parseInt(wtMonkey.left) > 150)) {
    
        dir = dir * -1;
    
        wtMonkey.zIndex = 5 - dir;
    
      }
    
      wtMonkey.left = parseInt(wtMonkey.left) + dir;
    
      setTimeout('moveMonkey(\'' + monkey + '\', ' + dir +')', 100);
    
    }

The name of the monkey is passed along to the function, which moves it by changing the left dimension of the monkey. It then does the standard feature check, setting the reference variable for the appropriate browser. It then changes the z-index and direction of movement, if the monkey has moved to either end of the desk. Finally, it moves the monkey and then calls the function again, passing the variables.

Keep in mind that this is only one way to do a generic JavaScript movement routine. You can feel free to add, subtract, or write your own. In fact, I encourage it. So here's the homework. Take act I, scene I, and animate it. Use either the code snippets provided or make up your own. Make the monkeys run around their desks, make them jump for joy, make them sing and shout. Feel free to make up your own plots and dialogs. When you're constructing your routines, just remember how Netscape nests positioned objects.





About Webmonkey | Contribute  | Send Feedback

Copyright © 1994-99 Wired Digital Inc. All rights reserved.