Pages

Friday 14 September 2012

How to open a Div as a popup at the center of a page using JQuery ?

Let say we have a div "popup_div" which we want to open as a popup at the center of the page. We can do the same by following the below steps.
Step-1: Add a Div to your page which you want to open as popup and give it as id="popup_div".

<div id="popup_div">
            <div style="width: 99%; height: 20px; background-color: #303030; font-family:   
                 Calibri;font-size: 14px; font-weight: bold; color: White; padding-left: 8px;  
                 vertical-align: middle">View Chart</div>
</div>

Step-2: Add the CSS style to your div "popup_div". 

<style type="text/css">
        .div_popup_
        {
            display: none;
            position: fixed;
            _position: absolute;
            height: 320px;
            width: auto;
            background: #FFFFFF;
            z-index: 100;
            margin-left: 15px;
            border: 2px solid #999999;
            padding: 6px;
            font-size: 15px;
            -moz-box-shadow: 0 0 5px #ff0000;
            -webkit-box-shadow: 0 0 5px #ff0000;
        }
    </style>

<div id="popup_div" class="div_popup_">
            <div style="width: 99%; height: 20px; background-color: #303030; font-family
                 Calibri;font-size: 14px; font-weight: bold; color: White; padding-left: 8px;   
                 vertical-align: middle">View Chart</div>
</div>

Step-3: Add the following JQuery Function to your page which will open the div as popup that also at the center of the page.

function ShowPopUp() {
            $('#popup_div').fadeIn(500);
//-- The following line is to open at the center of the page --//
            $('#popup_div').css({ top: '50%', left: '50%', margin: '-' + ($('#popup_div').height() / 2) + 'px 0 0 -' + ($('#popup_div').width() / 2) + 'px' });

        };
Step-4: Call this method whenever you want to open the div.

Output: In my example I had to open a popup showing a chart. That's why I had placed a Chart control to the div and wrote all the codes to make a chart control. After that when I clicked the button to open the popup, the output was like the following.


Regards,
Prajanuranjan....


No comments:

Post a Comment

Total Pageviews