简体   繁体   English

叠加框出现在屏幕中央

[英]Overlay box appearing in the center of the screen

I have some JS code that fades the background to a dark colour and opens a white modal window, like so: 我有一些JS代码将背景淡化成深色并打开一个白色模态窗口,如下所示:

function open_litebox(link) {
    var overlay_black = 'position:absolute;top:0%;left:0%;width:100%;height:100%;background:black;z-index:1001;opacity:0.5;';
    $('body').append('<div style="' + overlay_black + '"></div>');
    var overlay_white = 'position:absolute;top:25%;left:25%;padding:5px;background-color:white;z-index:1002;overflow:auto;';
    $('body').append('<div style="' + overlay_white + '"><p>heeeyyy</p></div>');
}

But the problem is, it doesn't appear dead center in the screen. 但问题是,它并没有出现在屏幕中死角。 I want the modal window to be positioned in the dead center both vertically and horizontally, but I don't know what the width/height of the content inside the modal window is going to be so I can't set fixed values. 我希望模态窗口在垂直和水平方向都位于死点,但我不知道模态窗口内的内容的宽度/高度是多少,所以我无法设置固定值。

Any help is appreciated, cheers. 欢迎任何帮助,欢呼。

You need to use these styles to make your white overlay appear dead-center : 您需要使用这些样式使白色叠加显示为死点

var overlay_white = 'position:absolute;
                     top:50%;
                     left:50%;
                     background-color:white;
                     z-index:1002;
                     overflow:auto;
                     width:400px;
                     height:400px;
                     margin-left:-200px;
                     margin-top:-200px';

So position should be specified. 所以应该指定position The top and left should be 50% . topleft应为50% The margin-left and margin-top should be negative one half of the width and height of the box respectively. margin-leftmargin-top应分别为框的宽度和高度的负半边。

I know this is an old question, but it came up while I was googling the topic, and I wanted to provide an updated answer. 我知道这是一个古老的问题,但是当我在谷歌上搜索这个主题时,我想提供一个更新的答案。

Expanding on Sarfraz's answer, you do not need to explicitly specify the width and height. 扩展Sarfraz的答案,您不需要明确指定宽度和高度。 If these are unknown (or you do not want to set them), you can use the following in your styling: 如果这些是未知的(或者您不想设置它们),则可以在样式中使用以下内容:

    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);

The transform line figures out the width and height for you. 变换线为您计算宽度和高度。

You should be able to get the width and height of an element after the content is loaded (try using jquery helpers for that - width() , height() ) 在加载内容后,您应该能够获得元素的宽度和高度(尝试使用jquery帮助器 - width()height()

Once you have the dimensions then the calculation can happen and then position the element. 获得尺寸后,可以进行计算,然后定位元素。 You can also keep the element hidden until the positioning has been done so the user does not see the jumpiness. 您还可以隐藏元素,直到完成定位,这样用户就不会看到跳跃。

You can also use a plugin like fancybox which makes things like this really simple. 您还可以使用像fancybox这样的插件,这样的事情非常简单。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM