Skip to content

Instantly share code, notes, and snippets.

@azeemhassni
Created August 18, 2014 10:27
Show Gist options
  • Save azeemhassni/2e116ac71cda77e1b8ce to your computer and use it in GitHub Desktop.
Save azeemhassni/2e116ac71cda77e1b8ce to your computer and use it in GitHub Desktop.
jQuery Center element method
// add Center method to jquery
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;
}
// Call
jQuery("element").center();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment