layer center js

layer center js

by jihoon kim

HTML

<table style="width: 100%; height: 20px;" border="2" cellspacing="0" cellpadding="0">
<tbody>
<tr style="height: 100px;">
<td style="height: 100px;" align="center">중앙</td>
</tr>
</tbody>
</table>

JavaScript

function init() {
    var obj = document.all['test'];
    var w = parseFloat(obj.style.width);
    var h = parseFloat(obj.style.height);
    //alert (w + ' / ' + h );
    //alert ( document.body.scrollTop );
    var aw = parseFloat(document.body.clientWidth);
    var ah = parseFloat(document.body.clientHeight);
    //alert (aw + ' / ' + ah );
    var t = (ah - h) / 2;
    var l = (aw - w) / 2;
    //alert (t + ' / ' + l);
    obj.style.top = t + parseFloat(document.body.scrollTop);
    obj.style.left = l + parseFloat(document.body.scrollLeft);
    //alert (obj.style.top + ' / ' + obj.style.left);
}
window.attachEvent('onload',init);
window.attachEvent('onresize',init);