Skip to content

Instantly share code, notes, and snippets.

@kalinchernev
Created December 11, 2014 12:47
Show Gist options
  • Save kalinchernev/106e76a9302d96e12c78 to your computer and use it in GitHub Desktop.
Save kalinchernev/106e76a9302d96e12c78 to your computer and use it in GitHub Desktop.
jQuery method to center an element on the window
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;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment