简体   繁体   中英

How can I center this JQuery dialog?

I was using a JQuery dialog like this: $(thisDialog).dialog(); until I learned that it doesn't like IE9 Quirks mode and doctype strict is not an option because of legacy code, how can I center the popup from this example?

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

A jQuery solution, considering the modal-dialog is position absolute/relative/fixed:

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 :)

A jQuery solution, considering the modal-dialog is not position absolute/relative/fixed:

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 :)

You should write something like this:

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>

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