使用jQuery在屏幕上居中放置DIV

如何设置<部门&gt

我喜欢向jQuery添加函数,因此此函数将有助于:

jQuery.fn.center=函数(){
css(“位置”、“绝对”);
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”);
归还这个;
}

现在我们可以写:

$(元素).center();

演示:小提琴(添加参数)

发表评论