开发者

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

开发者 https://www.devze.com 2023-03-29 22:51 出处:网络
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.

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

精彩评论

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