Welcome to WebmasterWorld Guest from 54.144.79.200

Forum Moderators: open

Message Too Old, No Replies

why can not use "style.offsetLeft"?

     

susky

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

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

WebmasterWorld Senior Member 5+ Year Member



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

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

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

WebmasterWorld Senior Member 5+ Year Member



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

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



thank you daveVK