繁体   English   中英

JavaScript弹出框位置

[英]Javascripts pop-up box position

我正在尝试实现一个javascript弹出框,但是我不确定如何确定弹出窗口的位置。 请检查以下小提琴。 当我单击按钮时,该框会以某种方式出现在页面中间。 有没有办法让它显示在我的按钮下方?

http://jsfiddle.net/kf9mS/

<html lang="en" dir="ltr">
<head>
<style type="text/css">
.popup{
    position:relative;
    top:0px;
    left:0px;
    margin:0px auto;
    width:200px;
    height:150px;
    font-family:verdana;
    font-size:13px;
    padding:10px;
    background-color:rgb(240,240,240);
    border:2px solid grey;
    z-index:100000000000000000;
    display:none
    }

.cancel{
    display:relative;
    cursor:pointer;
    margin:0;
    float:right;
    height:10px;
    width:14px;
    padding:0 0 5px 0;
    background-color:red;
    text-align:center;
    font-weight:bold;
    font-size:11px;
    color:white;
    border-radius:3px;
    z-index:100000000000000000;
    }

.cancel:hover{
    background:rgb(255,50,50);
    }

</style>
</head>
<body>
<button onClick="openPopup();">click here</button>
<div id="test" class="popup">
    This is a test message
    <div class="cancel" onclick="closePopup();"></div>
</div>
</body>
</html>

function openPopup() {
    document.getElementById('test').style.display = 'block';
}

function closePopup() {
    document.getElementById('test').style.display = 'none';
}

(由javascript onclick create(element)div viz弹出框派生)

谢谢!

在CSS popup position: absolute; remove top and left属性,使您的popup变为

.popup{
    position:absolute;
    margin:0px auto;
    width:200px;
    height:150px;
    font-family:verdana;
    font-size:13px;
    padding:10px;
    background-color:rgb(240,240,240);
    border:2px solid grey;
    z-index:100000000000000000;
    display:none
    }

休息都很好

删除margin: 0 auto属性。 因为它强制元素居中。

工作小提琴

暂无
暂无

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

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