jQuery Center Web Page或垂直和水平方向上任何元素的jQuery代码段。 可以与事件功能一起使用,即使在调整窗口大小时也可以始终在屏幕上居中放置网页。
jQuery.fn.center = function() {
this.css("position", "absolute");
this.css("top", ($(window).height() - this.height()) / 2 + $(window).scrollTop() + "px");
this.css("left", ($(window).width() - this.width()) / 2 + $(window).scrollLeft() + "px");
return this;
};
From: https://www.sitepoint.com/jquery-center-web-page-vertical-horizontal/