In my client apps I often have a need to center elements in a browser window. Specifically when I have pop ups come up for the first time or when special status messages that are popped onto the screen during long operations, or modal dialogs pop up it's often necessary to have the content centered in the browser window. All of those operations tend to require some sort of centering of content at least in its default configuration.
I've had a centering routine for a while, but thought I should simplify by using jQuery and adding a few features like the ability to center content inside of other container elements on a page. There are already a couple of center like plugins available, but neither of the two I tried worked properly for centering content both horizontally and vertically in the main window including catching for scroll position.
One of the really nice things about jQuery is the ease with which you can extend jQuery's functionality via plug-ins. So I created a quick plug-in that does the centering job a lot more easily than my old code did and it's more flexible to boot.
Using the plug-in is as easy as:
$(document).ready(function() {
$("#box").centerInClient();
});
which centers the element named box in the client window. To center the element inside of another container element you might use:
$("#box").centerInClient({ container: window, forceAbsolute: true });
Here's the code for my centerInClient plugin:
$.fn.centerInClient = function(options) {
/// <summary>Centers the selected items in the browser window. Takes into account scroll position.
/// Ideally the selected set should only match a single element.
/// </summary>
/// <param name="fn" type="Function">Optional function called when centering is complete. Passed DOM element as parameter</param>
/// <param name="forceAbsolute" type="Boolean">if true forces the element to be removed from the document flow
/// and attached to the body element to ensure proper absolute positioning.
/// Be aware that this may cause ID hierachy for CSS styles to be affected.
/// </param>
/// <returns type="jQuery" />
var opt = { forceAbsolute: false,
container: window, // selector of element to center in
completeHandler: null
};
$.extend(opt, options);
return this.each(function(i) {
var el = $(this);
var jWin = $(opt.container);
var isWin = opt.container == window;
// force to the top of document to ENSURE that
// document absolute positioning is available
if (opt.forceAbsolute) {
if (isWin)
el.remove().appendTo("body");
else
el.remove().appendTo(jWin.get(0));
}
// have to make absolute
el.css("position", "absolute");
// height is off a bit so fudge it
var heightFudge = isWin ? 2.0 : 1.8;
var x = (isWin ? jWin.width() : jWin.outerWidth()) / 2 - el.outerWidth() / 2;
var y = (isWin ? jWin.height() : jWin.outerHeight()) / heightFudge - el.outerHeight() / 2;
el.css("left", x + jWin.scrollLeft());
el.css("top", y + jWin.scrollTop());
// if specified make callback and pass element
if (opt.completeHandler)
opt.completeHandler(this);
});
}
The plug in can center elements either in the active window taking scroll positioning into account or center content inside of another container element.
There are several options available that can be passed using the option map.
container - optional container element to center in - defaults to window
forceAbsolute - forces the element to be centered to absolute positioning at the Body level. Use this if the element is already relatively positioned to another element and so force it to 'document' absolute positioning.
completeHandler - optional handler that is called when centering is complete. Use for trapping positioning notification.
I find this a handy feature to have around especially when working with pseudo windows and popups in client scripting.
Other Posts you might also like