如何设置 <div>
使用 jQuery 在屏幕中央?
最佳答案
我喜欢向 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;
}
现在我们可以写了:
$(element).center();
演示:Fiddle (添加参数)
关于javascript - 使用 jQuery 使 DIV 在屏幕上居中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/210717/