PDA

View Full Version : javascript to position absolute div


andehlu
05-06-2005, 03:30 PM
Does anyone know how to position an absolte div using javascript? I have the following code so far but it doesnt seem to work... i tried taking the top and left attributes out of the inlkine style and that didnt work either.... thanks


<html>
<head>

<title>2005 YOPLAIT Minigo Duo Campaign Online Advertising</title>

<script>
function handle_div(){
document.getElementById('div_layer').top = 345;
document.getElementById('div_layer').left = 345;
}
</script>

</head>
<body onLoad="handle_div()">

body content

<div id="div_layer" style="position: absolute; left: 400; top: 100; width: 200; height: 200; z-index: 1; visibility: visible;">
<img src="media/animated.gif">
</div>

</body>
</html>

andehlu
05-06-2005, 03:41 PM
this is why:

document.getElementById('div_layer').style.pixelLe ft = 200;

sleekdigital
05-06-2005, 03:41 PM
You are close ...

document.getElementById('div_layer').style.top = 345;
document.getElementById('div_layer').style.left = 345;

andehlu
05-06-2005, 04:51 PM
yup thanks i also had to target for IE

this works on everything:


if (navigator.appName.indexOf("Microsoft")!=-1) {
toppos = document.body.clientHeight / 2 - 100;
leftpos = document.body.clientWidth / 2 - 100;
}else{
toppos = window.innerHeight / 2 - 100;
leftpos = window.innerWidth / 2 - 100;
}