Možda
Kôd:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>test</title>
<script type="text/javascript">
var centerElement = function(element) {
var scrollX = document.documentElement.scrollLeft || document.body.scrollLeft;
var scrollY = document.documentElement.scrollTop || document.body.scrollTop;
var scWidth = function() {
var width = -1;
var mode = document.compatMode;
if(mode || window.ActiveXObject) {
switch(mode) {
case 'CSS1Compat':
width = document.documentElement.clientWidth;
break;
default:
width = document.body.clientWidth;
break;
}
} else {
width = self.innerWidth;
}
return width;
}();
var scHeight = function() {
var height = -1;
var mode = document.compatMode;
if((mode || window.ActiveXObject) && (navigator.userAgent.toLowerCase().indexOf('opera') == -1) ) {
switch(mode) {
case 'CSS1Compat':
height = document.documentElement.clientHeight;
break;
default:
height = document.body.clientHeight;
break;
}
} else {
height = self.innerHeight;
}
return height;
}();
var elemWidth = element.offsetWidth;
var elemHeight = element.offsetHeight;
var x = (scWidth - elemWidth) / 2 + scrollX;
var y = (scHeight - elemHeight) / 2 + scrollY;
element.style.position = 'absolute';
element.style.left = parseInt(x, 10) + 'px';
element.style.top = parseInt(y, 10) + 'px';
};
window.onload = function() {
var newDiv = document.createElement('div');
newDiv.id = 'floater';
newDiv.appendChild(document.createTextNode('ovo je test'));
document.body.appendChild(newDiv);
centerElement(newDiv);
}
window.onresize = function() {
if(document.getElementById('floater'))
centerElement(document.getElementById('floater'));
}
</script>
<style>
#floater {
background-color: #eee;
width: 110px;
padding: 5px;
border: 2px solid #ccc;
}
</style>
</head>
<body>
</body>
</html>