如何将这个JQuery对话框放在中心?

时间:2014-07-22 16:54:14

标签: jquery quirks-mode

我正在使用这样的JQuery对话框:$(thisDialog).dialog();直到我发现它不像IE9 Quirks模式而且doctype strict因为遗留代码而不是一个选项,我怎样才能将弹出窗口置于中心位置这个例子?

http://jsfiddle.net/46jYC/3/

2 个答案:

答案 0 :(得分:1)

jQuery解决方案,考虑模态对话框是绝对/相对/固定的位置:

var windowHeight = $(window).height();
var windowWidth = $(window).width();
var boxHeight = $('.modal-dialog').height();
var boxWidth = $('.modal-dialog').width();
$('.modal-dialog').css({'left' : ((windowWidth - boxWidth)/2), 'top' : ((windowHeight - boxHeight)/2)}); //change selector to whatever your selector is :)

jQuery解决方案,考虑到模态对话框不是绝对/相对/固定的位置:

的CSS:

margin-left: auto;
margin-right: auto;

jquery的:

var windowHeight = $(window).height();
var boxHeight = $('.modal-dialog').height();
$('.modal-dialog').css({'margin-top' : ((windowHeight - boxHeight )/2)});  //change selector to whatever your selector is :)

答案 1 :(得分:0)

你应该写这样的东西:

function abrirPopup(url,w,h) {
    var newW = w + 100;
    var newH = h + 100;
    var left = (screen.width - newW) / 2;
    var top = (screen.height - newH) / 2;
    var newwindow = window.open(url, 'name', 'width=' + newW +
            ',height=' + newH + ',left=' + left + ',top=' + top);
    newwindow.resizeTo(newW, newH);

    //posiciona o popup no centro da tela
    //(position the popup in the center of the canvas)
    newwindow.moveTo(left, top);
    newwindow.focus();
    return false;
}

HTML:

<a href="#" onclick="return abrirPopup('http://www.google.com.br', 500, 400)">Google</a>