Welcome to WebmasterWorld Guest from 54.167.46.29

Forum Moderators: open

Message Too Old, No Replies

why can not use "style.offsetLeft"?

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

New User

joined:June 15, 2011
posts:7
votes: 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?
4:06 am on June 20, 2011 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Nov 3, 2005
posts:1585
votes: 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.
2:56 pm on June 20, 2011 (gmt 0)

New User

joined:June 15, 2011
posts:7
votes: 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 June 21, 2011 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Nov 3, 2005
posts:1585
votes: 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.
5:04 am on June 22, 2011 (gmt 0)

New User

joined:June 15, 2011
posts:7
votes: 0


thank you daveVK
 

Join The Conversation

Moderators and Top Contributors

Hot Threads This Week

Featured Threads

Free SEO Tools

Hire Expert Members