0

jQuery 居中显示弹出层DIV

new created at6 years ago view count: 2486

有的时候弹出Loading或者提示什么的需要居中显示,使用下面的代码既可实现。

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();
report
回复

Recent search keywords