将弹出窗口设置为居中jquery

el.*_*ero 7 javascript jquery jquery-ui

现在我以这种方式设置模态jQuery窗口的位置:

   var winH = $(window).height();
   var winW = $(window).width();
    //Set the popup window to center
    $(id).css('top',  winH/2-$(id).height()/2);
    $(id).css('left', winW/2-$(id).width()/2);
Run Code Online (Sandbox Code Playgroud)

当我向下滚动时如何使popUp居中?

Mik*_* L. 8

您可以使用其他CSS样式,尝试 position: fixed


ari*_*man 5

你可以用这种方式使用它

//在页面中心放置模态框

    jQuery.fn.center = function () {
    this.css("position","absolute");
    this.css("top", ( jQuery(window).height() - this.height() ) / 2+jQuery(window).scrollTop() + "px");
    this.css("left", ( jQuery(window).width() - this.width() ) / 2+jQuery(window).scrollLeft() + "px");
    return this;
  }
Run Code Online (Sandbox Code Playgroud)

//然后调用该函数

 jQuery(".modal-profile").center();
Run Code Online (Sandbox Code Playgroud)

这将使您的代码组织化并易于在任何项目中使用以使模态居中.你可以在另一个问题上看到这种工作

模态出现在页面加载上