Javascript弹出框位置

时间:2013-02-07 04:15:35

标签: javascript html css ajax

我正在尝试实现一个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 popup box分叉)

谢谢!

2 个答案:

答案 0 :(得分:0)

在您的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
    }

休息一切都很好

答案 1 :(得分:0)

删除margin: 0 auto属性。因为它强制要素位于中心。

Working Fiddle