16

我有以下代码在禁用背景的同时打开一个新的弹出窗口,问题是我必须定位它,使其距离顶部 100px(已经通过 CSS #dialog 得到)并且也在屏幕的中心,不管用户的分辨率是多少?

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
    <script type="text/javascript">
        function showPopUp(el) {
            var cvr = document.getElementById("cover")
            var dlg = document.getElementById(el)
            cvr.style.display = "block"
            dlg.style.display = "block"
            if (document.body.style.overflow = "hidden") {
                cvr.style.width = "1024"
                cvr.style.height = "100%"
            }
        }
        function closePopUp(el) {
            var cvr = document.getElementById("cover")
            var dlg = document.getElementById(el)
            cvr.style.display = "none"
            dlg.style.display = "none"
            document.body.style.overflowY = "scroll"
        }
    </script>
    <style type="text/css">
        #cover {
            display:        none;
            position:       absolute;
            left:           0px;
            top:            0px;
            width:          100%;
            height:         100%;
            background:     gray;
            filter:         alpha(Opacity = 50);
            opacity:        0.5;
            -moz-opacity:   0.5;
            -khtml-opacity: 0.5
        }

        #dialog {
            display:    none;
            left:       100px;
            top:        100px;
            width:      300px;
            height:     300px;
            position:   absolute;
            z-index:    100;
            background: white;
            padding:    2px;
            font:       10pt tahoma;
            border:     1px solid gray
        }
    </style>
</head>
<body>
<div id="cover"></div>
<div id="dialog">
    My Dialog Content
    <br><input type="text">
    <br><input type="button" value="Submit">
    <br><a href="#" onclick="closePopUp('dialog');">[Close]</a>
</div>
<a href="#" onclick="showPopUp('dialog');">Show</a>

</body>
</html>
4

7 回答 7

46

基于 CSS 的中心解决方案:

您需要使用这些样式使其看起来死心:

position:absolute;
top:50%;
left:50%;
width:400px;  /* adjust as per your needs */
height:400px;   /* adjust as per your needs */
margin-left:-200px;   /* negative half of width above */
margin-top:-200px;   /* negative half of height above */

所以position应该指定。和top应该left50%margin-leftmargin-top应该分别是盒子宽度和高度的负值的一半。

请注意,如果您希望您的弹出窗口即使在页面滚动时也出现在中心,您将不得不使用position:fixed它在 IE6 中不起作用的回退。

于 2012-06-13T19:16:50.687 回答
9

只需这样做:

.body {
    position: relative;
}
.popup {
    position: absolute;
    max-width: 800px;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
}

无论屏幕或弹出窗口大小。这将使<div class="popup"></div>.

于 2016-02-18T15:42:12.073 回答
3

你需要的是一个灯箱

要创建它,您应该修改 HTML、CSS 和 JS 代码。

假设您的灯箱仅包含字符串“登录表单”。(你可以把你想要的一切都放在那里) HTML 代码应该是这样的:

<div id = "loginBox">login form</div>

现在,我们需要用 CSS 隐藏它:

div#loginBox {
    display: none;
}

现在我们的盒子是不可见的。让我们修改我们的框,使其距离顶部 100px:

div#loginBox {
    display: none;
    top: 100px;
}

我们稍后会担心禁用背景。

我们的下一个工作是制作一个按钮,在需要时显示该框。十分简单:

<div id = "loginBox" >login form</div>
<a id = "displayButton">login</a>

请注意,我们不需要“href”属性,因为这会在点击和其他不需要的行为时移动屏幕。

让我们通过 JS 在按钮上附加事件处理程序:

var IE = document.all ? true : false; // obligatory "browser sniffing"

function display_box() {
    document.getElementsById("loginBox").style.display = "inline-block"; // or "inline" 
}

window.onload = function() {
    var login_box = document.getElementsById("loginBox");
    if (!IE) {
        login_box.addEventListener( "click", display_box , false );
    }
    else {
        login_box.attachEvent( "onclick", display_box );
    }
}

但是您希望它位于屏幕中央吗?然后函数是这样的:

function display_box() {
    var theBox = document.getElementsById("loginBox").style,
        left = document.width / 2 - 50;   // 150 because it is 300 / 2

    theBox.display = "inline-block";
    theBox.left = left.toString() + "px";
}

我猜你会想在某个时候关闭窗口并制作“禁用背景”效果。为此,您可以创建一个在整个屏幕上扩展的 div 类,在其上附加一个“显示”事件,在 css 中放置一些 z-index 以确保 loginBox 位于“禁用背景”之上,并附加一个“关闭“背景”div 上的 loginBox”事件。现在最终的代码如下所示:

请注意,我们只关心登录按钮的位置,因为其他按钮是隐藏的,然后由 JS 修改:

HTML:

<div id = "loginBox" >login</div>
<a id = "displayButton">login</a>
<div id = "backgroundDarkener"> </div>

CSS:

div#loginBox {
    display: none;
    top: 100px;
    width: 300px; #it is important to know the width of the box, to center it correctly
    z-index: 2;
}
div#backgroundDarkener {
    background: #000;
    display: none;
    position: fixed;
    top: 0px;
    left: 0px;
    width: 100%;
    height: 100%;
    z-index: 1;
    opacity: 0.8; 
    # needless to say, you should play with opacity or if you want your
    # css to validate - background image (because I suspect it won't 
    # validate for old versions of IE, Safari, etc.) This is just a suggestion
}

JS:

var IE = document.all ? true : false; // obligatory "browser sniffing"

function display_box() {
    var theBox = document.getElementsById("loginBox").style,
        background = document.getElementsById("loginBox").style,
        left = document.width / 2 - 150;   // 150 is 300 / 2

    theBox.display = "inline-block";
    theBox.left = left.toString() + "px";
    background.display = "inline-block";
}
function hide_box() {
    document.getElementsById("loginBox").style.display = "none";
    document.getElementsById("backgroundDarkener").style.display = "none"; 
}

window.onload = function() {
    var login_box = document.getElementsById("loginBox"),
        background = document.getElementsById("backgroundDarkener");

    if (!IE) {
        login_box.addEventListener( "click", display_box , false );
        background.addEventListener( "click", hide_box , false );
    }
    else {
        login_box.attachEvent( "onclick", display_box );
        background.attachEvent( "onclick", hide_box );
    }
}
于 2012-06-13T20:06:53.017 回答
2

这就是 flexbox 现在来救援的地方!

.parent {
  display: flex;
  height: 300px; /* Or whatever */
}

.child {
  width: 100px;  /* Or whatever */
  height: 100px; /* Or whatever */
  margin: auto;  /* Magic! */
}
于 2015-02-09T19:26:06.823 回答
2

一个快速的谷歌搜索发现了这个

function PopupCenter(pageURL, title,w,h) {
  var left = (screen.width/2)-(w/2);
  var top = (screen.height/2)-(h/2);
  var targetWin = window.open (pageURL, title, 'toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=no, resizable=no, copyhistory=no, width='+w+', height='+h+', top='+top+', left='+left);

} 
于 2012-06-13T19:15:15.570 回答
1

您需要使用这些样式来使 div 居中:

width:500px; 
left:0;
right:0;
margin:0 auto;
于 2014-05-27T06:11:27.053 回答
0

很简单,margin: 100px auto;。无需在 JavaScript 中进行计算。

现场示例

于 2012-06-13T19:15:58.633 回答