Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    Regular Coder
    Join Date
    Oct 2002
    Posts
    299
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Getting and setting px and % as expression or stylesheet value gives difference

    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

  • #2
    Regular Coder
    Join Date
    Oct 2003
    Location
    SC
    Posts
    936
    Thanks
    0
    Thanked 0 Times in 0 Posts
    mhhh, I think it would be better if you use .offsetTop to get the position...

  • #3
    Regular Coder
    Join Date
    Oct 2002
    Posts
    299
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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').offsetL eft/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

  • #4
    Regular Coder
    Join Date
    Oct 2003
    Location
    SC
    Posts
    936
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Unhappy

    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
    )

    have fun coding


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •