Here's a function that I wrote to find the coordinates of the center of the page.
Code:
function findVisiblePageCenter() {
var x1,y1,x2,y2,d=document,coords={};
// function by Dynamic Site Solutions -- http://www.dynamicsitesolutions.com/
// much of the code in this function came from
// http://www.quirksmode.org/viewport/compatibility.html
if(self.innerHeight) { // all except Explorer
x1 = self.innerWidth;
y1 = self.innerHeight;
}
else if(d.documentElement && d.documentElement.clientHeight) {
// Explorer 6 Strict Mode
x1 = d.documentElement.clientWidth;
y1 = d.documentElement.clientHeight;
}
else if(d.body) { // other Explorers
x1 = d.body.clientWidth;
y1 = d.body.clientHeight;
}
if(self.pageYOffset) { // all except Explorer
x2 = self.pageXOffset;
y2 = self.pageYOffset;
}
else if(d.documentElement && d.documentElement.scrollTop) {
// Explorer 6 Strict
x2 = d.documentElement.scrollLeft;
y2 = d.documentElement.scrollTop;
}
else if(d.body) { // all other Explorers
x2 = d.body.scrollLeft;
y2 = d.body.scrollTop;
}
if(!isNaN(y2) && !isNaN(x2)) {
coords.x = Math.floor(x1/2 + x2);
coords.y = Math.floor(y1/2 + y2);
} else {
coords.x = Math.floor(x1/2);
coords.y = Math.floor(y1/2);
}
return coords;
}
Bookmarks