How do I vertically center a modal using jQuery, independent of scrolling height?

为君一笑 提交于 2019-12-25 16:10:04

问题


I want to vertically center a modal. If the window height is sufficient to require scrolling, the popup always appears near the top. This means I will need to scroll up to see it.

var winH = $(window).height(),
    winW = $(window).width();
$(".popup").css('top', winH / 2 - $(".popup").height() / 2);
$(".popup").css('left', winW / 2 - $(".popup").width() / 2);

CSS:

.popup {
    overflow:hidden;
    position:absolute;
    width:600px;
}

回答1:


Just go with "position: fixed".




回答2:


Sounds like you already found your answer, but since I have it. Here's a working JS fiddle version: Live Version



来源:https://stackoverflow.com/questions/7127591/how-do-i-vertically-center-a-modal-using-jquery-independent-of-scrolling-height

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!