简体   繁体   中英

javascript to overlay a modal popup that is center aligned

want to know Simply the javascript to overlay a div on centre of the page.

Just want to use plain java script to show and hide a div on the center of the page with "Please wait..." message and disable the background. Also this div shoud show on top of the other content of the page.

My div looks like this

<div id='mydiv' style="display:none;" ><img src="myimg.gif" /> Please Wait... </div>

On click of a button , I want to show the div content center aligned on the page.

I do not want to use jquery,extjs,,etc to achieve this.

I have seen a few examples on the web with lot of other features added to a modal popup, just looking for something simple and clean.The bare minimum JS required to do this.

The div you want to display needs to have an ID:

<div id="loaderdiv">

Then in your javascript, you display this div with the following code:

document.getElementById("loaderdiv").style.display = '';

Thats the bare minimum you'll need.

Centering the image can be done using CSS:

<div style="position:absolute;top:50%;left:50%;margin-top:-[imgheight/2]px;margin-left:-[imgwidth/2]px">
<div class="overlay_msg" id="overlay_msg" style="width:350px; height:100px; background-color:#ffffff; margin-left:300px; margin-top:20%; visibility:hidden; z-index:201; position:fixed; padding:15px; text-align:center;">
                <a href="http://example.com">example.com</a><br />
        </div><!--overlay_msg-->
        <div class="my_overlay" id="my_overlay" style="background-color:#000000; opacity:.7; position:fixed; z-index:200; width:100%; height:100%; margin:0px; padding:0px; visibility:hidden;" onclick="hideMyOverlay()">
        </div><!--my_overlay-->
        <script type="text/javascript">
            function showMyOverlay()
            {
                document.getElementById('my_overlay').style.visibility='visible';
                document.getElementById('overlay_msg').style.visibility='visible';
            }
            function hideMyOverlay()
            {
                document.getElementById('my_overlay').style.visibility='hidden';
                document.getElementById('overlay_msg').style.visibility='hidden';               
            }
        </script>

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