简体   繁体   中英

Javascripts pop-up box position

I am trying to implement a javascript pop-up box, but I am not sure how to fix the position of the pop-up window. Please check the below fiddle. When I click on the button, the box somehow appears in the middle of the page. Is there a way to make it appears right under my button?

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>

and

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

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

(forked from javascript onclick create(element) div viz popup box )

Thanks!

In your CSS popup make position: absolute; and remove top and left property so your popup would become

.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
    }

Rest all is fine

Remove margin: 0 auto property. Because It is enforcing the element to be in center.

Working Fiddle

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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