Download the X Distribution File.
xCenter(e[, w[, h]])
Default.
// xCenter r1, Copyright 2009 Arthur Blake (http://arthur.blake.name)
// Part of X, a Cross-Browser Javascript Library, Distributed under the terms of the GNU LGPL
/**
* Center a positioned element within the current client window space.
*
* If w,h not specified, then the existing width and height of e are used.
*
* @param e an existing absolutely positioned dom element (or an id to such an element)
* @param w (optional) width to resize element to
* @param h (optional) height to resize element to
*/
function xCenter(e, w, h)
{
var ww=xClientWidth(),wh=xClientHeight(),x=0,y=0;
e = xGetElementById(e);
if (e)
{
w = w || xWidth(e);
h = h || xHeight(e);
if (ww < w)
{
w = ww;
}
else
{
x = (ww - w) / 2;
}
if (wh < h)
{
h = wh;
}
else
{
y = (wh - h) / 2;
}
// adjust for any scrolling
x += xScrollLeft();
y += xScrollTop();
xResizeTo(e, w, h);
xMoveTo(e, x, y);
}
}
xGetElementById, xClientWidth, xClientHeight, xWidth, xHeight, xScrollLeft, xScrollTop, xResizeTo, xMoveTo
toys - Index of all X demos.
By your use of X and/or CBE and/or any Javascript from this site you consent to the GNU LGPL - please read it. If you have any questions about the license, read the FAQ and/or come to the forums.
Forum support is available at the X Library Support Forums.
Cross-Browser.com is the home of X - a cross-browser Javascript library, and many demos, applications, articles and documentation.
If you are using X, XC or anything from this site, show off your work by posting a link in the X Showcase forum.