...

View Full Version : Getting and setting px and % as expression or stylesheet value gives difference



Jerome
03-10-2004, 01:22 PM
Hi,

I like to move an absolute positioned div --relative with respect to the body width-- horizontaly, without losing the relative respect to the body width.

Moving --mydiv-- works well, the problem I experience however, seems to be getting the correct left values (see point 1) and saving the new values (see point 2)

#mydiv{
position:absolute;
left:20%;
}


1.
Getting the current distance with:

document.getElementById('mydiv').style.left

or:

document.getElementById('mydiv').currentStyle.left

always gives the value in px, even if the stylesheet declaration is in percentage e.g. 20% can give 130px.


2.
The --mydiv-- is moved to another place, time to save the new left distance:

posibility A.

document.getElementById('mydiv').style.left=the new value+"%";

works perfect the first time the --mydiv-- is moved but the second time when getting the current distance (see point 1) the current distance f.e. 10% isn't 65px but 10px???

Posibility B.

document.getElementById('mydiv').style.setExpression("left",distancevalue);

Doesn't except percentages as value??


Thanks for Your time and help,
Jerome

JAVAEOC
03-10-2004, 11:26 PM
mhhh, I think it would be better if you use .offsetTop to get the position...

Jerome
03-11-2004, 12:24 PM
Hi JAVAEOC,

Your suggestion already helped a lot, do You have a solution for this as well?

mydiv.style.setExpression("left","document.body.clientWidth*0.10");

works well for me because it is,

mydiv.style.setExpression("left","VARIABEL*CONSTANTE");


The CONSTANT however, is (document.getElementById('mydiv').offsetLeft/document.body.clientWidth) on the moment I set the expression, which results in:


mydiv.style.setExpression("left","document.body.clientWidth*document.getElementById('mydiv').offsetLeft/document.body.clientWidth");

and yes then it doesn't work anymore, because I get,

mydiv.style.setExpression("left","VARIABEL*VARIABEL/VARIABEL");



How can I get a constant value from (document.getElementById('mydiv').offsetLeft/document.body.clientWidth) before I set the expression

Thanks
Jerome

JAVAEOC
03-11-2004, 06:18 PM
I do not know how to use the setExpresion thing but maybe this will help:

var Constant=document.getElementById('mydiv').offsetLeft/document.body.clientWidth

mydiv.style.setExpression("left","document.body.clientWidth*Constant")

hopefully this helps but i do not know how do use setExpression (lol, i dont even know what it does
:rolleyes: )

have fun coding :D



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum