Netscape 6, Part II: Animation: Coordinates - Doc JavaScript | WebReference

Netscape 6, Part II: Animation: Coordinates - Doc JavaScript


Netscape 6, Part II: Animation

Coordinates

The browser-independent W3C Standard's way to set and get an element's position is via the STYLE object's left and top properties. These properties denote physical measurements. Physical measurements are used either in HTML's STYLE attribute or in JavaScript. In HTML, they are numeric properties. Numeric properties comprised of a number, followed by units of measure. The following numeric properties are example values of the left and top properties: 50px, 32px, and 0px. As JavaScript does not support the "numeric property" data type, these measurements become strings when you extract them into JavaScript variables. The string includes a number followed by the string "px". The following strings are example values of the left and top properties: "50px", "32px", and "0px". When setting these properties in JavaScript, concatenate the "px" string to the value. For example, to set the left property of an element with ID="counter1" to xlocation, you will write:

document.getElementById('counter1').style.left = xlocation + "px";

The browser won't complain if you omit the "px" string:

document.getElementById('counter1').style.left = xlocation;

The browser assumes the unit of measure is pixels and will add the "px" automatically. You'll always get a "px"-ended string when querying the top and left properties.

It's very important to remember that although the STYLE object's left and top properties denote physical measurements, they are strings in JavaScript. Fail to do so, and you will get into long debugging sessions. When you need the pure numeric value of either left or top, use the parseInt() method. This method parses a string from left to right, converting the string to an integer. Parsing stops when there is no way to make an integer out of the string. For example, parseInt("50px") is 50. The following code segment adds 50 pixels to the button's left property, every time the user clicks the button:

<FORM>
<INPUT ID="counter1" STYLE="position:relative; left:0px"
  TYPE="button" VALUE="Move Button"
onclick="handleClick()">
</FORM>
<SCRIPT LANGUAGE="JavaScript">
<!--
var obj = document.getElementById('counter1');
var xlocation = parseInt(obj.style.left);
alert(xlocation);
function handleClick() {
  xlocation += 50;
  document.getElementById('counter1').style.left = xlocation + "px";
}
// -->
</SCRIPT>

Try it repetitively:

Notice that numeric operations (incrementing by 50) are done with the integer-typed xlocation. We set the left value by assembling xlocation and "px".

You can initialize an element properties by the STYLE attribute of its HTML tag. Notice the left:250px entry in the following tag definition:


<INPUT ID="button1" STYLE="position:relative; left:250px;
  visibility:visible;" TYPE="button" VALUE="Show My Visibility"
  onclick="handleClick1()">

Here is how this button renders:

Notice that numeric properties are written without any quotes. They are not strings in HTML's STYLE attribute.

Next: How to set, get, initialize, and manipulate element visibility

http://www.internet.com


Produced by Yehuda Shiran and Tomer Shiran
All Rights Reserved. Legal Notices.
Created: December 18, 2000
Revised: December 18, 2000

URL: http://www.webreference.com/js/column73/6.html