December 12, 2000 - Moving Objects Vertically | WebReference

December 12, 2000 - Moving Objects Vertically

Yehuda Shiran December 12, 2000
Moving Objects Vertically
Tips: December 2000

Yehuda Shiran, Ph.D.
Doc JavaScript

One of the trickiest tasks in DHTML is to move objects on the screen, independent of the browser. Let's focus on the vertical location of an object, usually associated with the word top. Netscape Navigator uses the top property of an element, while Internet Explorer uses the pixelTop property of the style object property. Netscape 6 uses the W3C Standard's top property of the style object property. Since Internet Explorer 5+ supports this property as well, we can use the following browser-independent code to move objects vertically:

<FORM>
<INPUT ID="counter1" STYLE="position:relative; top:'0px'" TYPE="button" VALUE="Move Button" 
onclick="document.getElementById('counter1').style.top = '15px';">
</FORM>

That renders like this:

Click the button and see it move down by 15 pixels. Notice that the measurement (15) is specified with its units of measure (pixels). If you try to drop the px, you will get the same behavior. Here is the HTML above without px:

<FORM>
<INPUT ID="counter2" STYLE="position:relative; top:0" TYPE="button" VALUE="Move Button" 
onclick="document.getElementById('counter2').style.top = 15;">
</FORM>

And you can play with it as well: