homepage Welcome to WebmasterWorld Guest from
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
Forum Library, Charter, Moderator: open

JavaScript and AJAX Forum

why can not use "style.offsetLeft"?

 4:35 pm on Jun 19, 2011 (gmt 0)

Hello i am a new comer
i want to make a "div" move,but i dont't understand why why can not use "style.offsetLeft"?
here is code:

if i write such as :block.style.left=block.style.left+step+'px';

the result is wrong!

can you tell me why the "block.style.left" is wrong?



 4:06 am on Jun 20, 2011 (gmt 0)


block is style.position='static' by default, in which case style.left has no meaning. set to 'relative' or 'absolute' if you wish the div to be moved and sized independent of other content.

style.offsetLeft is invalid, it is an element property NOT a style property.


 2:56 pm on Jun 20, 2011 (gmt 0)

thank you daveVk!
i wanted to write "style.left",but i did't pay attention
and write "style.offsetLeft"

if use "style.left",must write "parsetInt(block.style.left)"
for the value of "style.left" be a string


 2:34 am on Jun 21, 2011 (gmt 0)

block.style.left will have a value like 30px, parseInt will get the numeric part, that is 30



ends up with say 40px.

Note that the first + means addition ( as parseInt returns a number and step is a number )

The second addition is a string concat as 'px' is a string.

So there is more going on in this statement (and potential for error)
than may be apparent.

This assumes style.left initially has value in pixels and not say 2em.


 5:04 am on Jun 22, 2011 (gmt 0)

thank you daveVK

Global Options:
 top home search open messages active posts  

Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved