开发者

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

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 = $(wi开发者_JS百科ndow).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;
}


Just go with "position: fixed".


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜