homepage Welcome to WebmasterWorld Guest from 54.227.25.58
register, free tools, login, search, subscribe, help, library, announcements, recent posts, open posts,
Subscribe to WebmasterWorld
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"?
susky




msg:4327991
 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:
block.style.left=block.offsetLeft+step+'px';

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?

 

daveVk




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

Hi

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.

susky




msg:4328283
 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

daveVk




msg:4328570
 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

So

block.style.left=parseInt(block.style.left)+step+'px';

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.

susky




msg:4329187
 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