Thursday, 1 March 2012

Centering a Div in the centre of the Screen Using JQuery

jQuery.fn.center = function () {
    this.css("position","absolute");
    this.css("top", (($(window).height() - this.outerHeight()) / 2) + $(window).scrollTop() + "px");
    this.css("left", (($(window).width() - this.outerWidth()) / 2) + $(window).scrollLeft() + "px");
    return this;
}


//We can call the method as follows:
$("#popup").center();

No comments:

Post a Comment