Using jQuery to center a DIV on the screen

……

……

$(window).resize(function () { $(the_element).center() });
$(document).ready(function () { $(the_element).center() });

// http://stackoverflow.com/questions/210717/using-jquery-to-center-a-div-on-the-screen
jQuery.fn.center = function () {
this.css(“position”,”absolute”);
this.css(“top”, Math.max(0, (($(window).height() – $(this).outerHeight()) / 2) +
$(window).scrollTop()) + “px”);
this.css(“left”, Math.max(0, (($(window).width() – $(this).outerWidth()) / 2) +
$(window).scrollLeft()) + “px”);
return this;
}

……

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s