spacer
Yehuda Shiran December 11, 2000
Moving Objects Horizontally
Tips: December 2000

Yehuda Shiran, Ph.D.
Doc JavaScript

Developer News
Microsoft Shows Off Silverlight 4, IE9 Plans
Metasploit Expands Vulnerability Test Framework
HyperCard Reborn?

One of the trickiest task in DHTML is to move objects on the screen, browser-independently. Let's focus on the horizontal location of an object, usually associated with the word left. Netscape Navigator uses the left property of an element. Internet Explorer uses the pixelLeft property of the style object property. Netscape 6 uses the W3C Standard's left 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 horizontally:

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

That renders like this:

Click the button and see it moving to the right 500 pixels. Notice that the measurement (500) 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; left:0" TYPE="button" VALUE="Move Button" 
onclick="document.getElementById('counter2').style.left = 500;">
</FORM>

And you can play with it as well:


People who read this tip also read these tips:

Look for similar tips by subject:

internet.commediabistro.comJusttechjobs.comGraphics.com

Search:

WebMediaBrands Corporate Info

Legal Notices, Licensing, Permissions, Privacy Policy.
Advertise | Newsletters | Shopping | E-mail Offers | Freelance Jobs

webref The latest from WebReference.com Browse >
Rolling Out Your Own HTML Application Version Control · HTML 5: Client-side Storage · Working with Ajax Server Extensions
Sitemap · Experts · Tools · Services · Email a Colleague · Contact FREE Newsletters 
 The latest from internet.com
Wi-Fi Product Watch, November 2009 · Chip Market Recovering From '08 Collapse · Low-Cost Tools to Kickstart Your New Business