views:

37

answers:

2

I have two divs that I need to position horizontally dependent on the width of the user's screen. I've styled their vertical position in CSS, and I am trying to position them horizontally using Javascript.

My divs:

<div id="tl">
     blah blah
</div>
 <div id="bl">
     blah blah
</div>

My CSS:

#tl {
position: absolute;
top: -14px;
right: 0;
}
#bl {
position: absolute;
bottom: 1px;
right: 0;
}

My Javascript:

var tl = document.getElementById('tl');
var bl = document.getElementById('bl');
var wide = parseInt(screen.width);
var nudge = wide*.86;
nudge = nudge+21;
tl = tl.style;
tl.right = ( parseInt(tl.right) + nudge );
bl = bl.style;
bl.right = ( parseInt(bl.right) + nudge );

However... nothing happens. No errors, and definitely no movement from my divs. What am I doing wrong? Can anyone help?

+1  A: 

jquery makes this very easy http://jquery.com/

$("#t1").css( {"right" : $("#t1").position().right+$(window).width()*0.86+21} );
rzzmttzz
I'm not sure, but don't you need to append "px" string? i.e $("#t1").css("right", "400px"); ?
I've been putting off learning JQuery... but I guess it's time.Thanks, I think I'll try this.
Polyonymy
jquery is crucial for painless javascript development
rzzmttzz
+1  A: 

Make your 'right' style inline:

<div id="tl" style="right:0">
    blah blah
</div>
<div id="bl" style="right:0">
    blah blah
</div>
Jeaffrey Gilbert
Didn't have any effect...
Polyonymy
Make script as function and call on <body OnLoad=""> or put your script (without function) below your related elements.
Jeaffrey Gilbert