1

I want to create a new layer including a login form (or other features), and I want to display it at center of the screen (depending on the screen's resolution and content).

This is the code :

  function new_layer() {
    $('#frameContainer, #showbox').show();
    $('#showbox').load('includes/login.php');
    $(document).ready(function(){
        var hei = eval($(window).height()/2 - $('#showbox > *').outerHeight()/2);
        var wid = eval($(window).width()/2 - $('#showbox > *').outerWidth()/2);
        $('#showbox').css({
          top: hei + "px",
          left: wid + "px"
        }).show();
    });

    // ..................

At the first time, it does not work, the content of the file login.php is centered :( , but when I click outside to hide it, then click again, it works. When I refresh the page, the problem occurs again.

What is the problem and how can i fix it?

Littm
  • 4,923
  • 4
  • 30
  • 38
nguyen
  • 7
  • 2
  • 2
    Why are you using `eval`? It's not only naughty, it's not even necessary in your code. – Blazemonger Sep 04 '12 at 14:27
  • Sr i'm a beginner, and i'm a cautious person, i try to do things safely :) – nguyen Sep 04 '12 at 14:36
  • If you want to be safe, then [never use `eval` in your code until you're 100% sure you know what you're doing](http://stackoverflow.com/questions/197769/when-is-javascripts-eval-not-evil). – Blazemonger Sep 04 '12 at 14:36

2 Answers2

0
$(function(){
var loginDialog = $('#loginDialog');

    $.blockUI();
    var dlgWidth = loginDialog.outerWidth();
    var viewPortWidth = $(window).width();
    var pos = (viewPortWidth - dlgWidth ) / 2;
    loginDialog.css({

        'position', 'absolute',
        'left' :  pos + 'px',
        'z-index' : '1999' //greater than setting for blockUI
    });

});

More on blockUI here

Robin Maben
  • 22,194
  • 16
  • 64
  • 99
0

You don't need JS calculations for that:

#loginDialog {
    height: 300px;
    width: 400px;
    position: absolute;
    top: 50%;
    left: 50%;
    margin-top: -150px; /* half of the height */
    margin-left: -200px; /* half of the width */
}
Zoltan Toth
  • 46,981
  • 12
  • 120
  • 134