How to center a popup window?

后端 未结 3 1692
无人及你
无人及你 2020-12-30 08:42

Center a popup window on screen? these don\'t work in Chrome w/ multimonitor. The \"screen\" seems to refer to the entire desktop, not just the current window. I want to cen

3条回答
  •  粉色の甜心
    2020-12-30 09:01

    Here's my method (requires jQuery):

    function OpenWindow(params, width, height, name) {
        var screenLeft=0, screenTop=0;
    
        if(!name) name = 'MyWindow';
        if(!width) width = 600;
        if(!height) height = 600;
    
        var defaultParams = { }
    
        if(typeof window.screenLeft !== 'undefined') {
            screenLeft = window.screenLeft;
            screenTop = window.screenTop;
        } else if(typeof window.screenX !== 'undefined') {
            screenLeft = window.screenX;
            screenTop = window.screenY;
        }
    
        var features_dict = {
            toolbar: 'no',
            location: 'no',
            directories: 'no',
            left: screenLeft + ($(window).width() - width) / 2,
            top: screenTop + ($(window).height() - height) / 2,
            status: 'yes',
            menubar: 'no',
            scrollbars: 'yes',
            resizable: 'no',
            width: width,
            height: height
        };
        features_arr = [];
        for(var k in features_dict) {
            features_arr.push(k+'='+features_dict[k]);
        }
        features_str = features_arr.join(',')
    
        var qs = '?'+$.param($.extend({}, defaultParams, params));
        var win = window.open(qs, name, features_str);
        win.focus();
        return false;
    }
    

    Seems to work in all browsers.

提交回复
热议问题