Skip to content

Instantly share code, notes, and snippets.

@koenhendriks
Created December 27, 2014 22:05
Show Gist options
  • Save koenhendriks/41fc077f3c4d7f86dae8 to your computer and use it in GitHub Desktop.
Save koenhendriks/41fc077f3c4d7f86dae8 to your computer and use it in GitHub Desktop.
jQuery center extension
$.fn.extend({
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;
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment