Center a popup window on screen?

前端 未结 18 766
一整个雨季
一整个雨季 2020-11-22 16:51

How can we center a popup window opened via javascript window.open function on the center of screen variable to the currently selected screen resolution ?

18条回答
  •  有刺的猬
    2020-11-22 17:32

    (this was posted in 2020)

    An extension to CrazyTim's answer

    You can also set the width to a percentage (or a ratio) for a dynamic size. Absolute size is still accepted.

    function popupWindow(url, title, w='75%', h='16:9', opts){
        // sort options
        let options = [];
        if(typeof opts === 'object'){
            Object.keys(opts).forEach(function(value, key){
                if(value === true){value = 'yes';}else if(value === false){value = 'no';}
                options.push(`${key}=${value}`);
            });
            if(options.length){options = ','+options.join(',');}
            else{options = '';}
        }else if(Array.isArray(opts)){
            options = ','+opts.join(',');
        }else if(typeof opts === 'string'){
            options = ','+opts;
        }else{options = '';}
    
        // add most vars to local object (to shorten names)
        let size = {w: w, h: h};
        let win = {w: {i: window.top.innerWidth, o: window.top.outerWidth}, h: {i: window.top.innerHeight, o: window.top.outerHeight}, x: window.top.screenX || window.top.screenLeft, y: window.top.screenY || window.top.screenTop}
    
        // set window size if percent
        if(typeof size.w === 'string' && size.w.endsWith('%')){size.w = Number(size.w.replace(/%$/, ''))*win.w.o/100;}
        if(typeof size.h === 'string' && size.h.endsWith('%')){size.h = Number(size.h.replace(/%$/, ''))*win.h.o/100;}
    
        // set window size if ratio
        if(typeof size.w === 'string' && size.w.includes(':')){
            size.w = size.w.split(':', 2);
            if(win.w.o < win.h.o){
                // if height is bigger than width, reverse ratio
                size.w = Number(size.h)*Number(size.w[1])/Number(size.w[0]);
            }else{size.w = Number(size.h)*Number(size.w[0])/Number(size.w[1]);}
        }
        if(typeof size.h === 'string' && size.h.includes(':')){
            size.h = size.h.split(':', 2);
            if(win.w.o < win.h.o){
                // if height is bigger than width, reverse ratio
                size.h = Number(size.w)*Number(size.h[0])/Number(size.h[1]);
            }else{size.h = Number(size.w)*Number(size.h[1])/Number(size.h[0]);}
        }
    
        // force window size to type number
        if(typeof size.w === 'string'){size.w = Number(size.w);}
        if(typeof size.h === 'string'){size.h = Number(size.h);}
    
        // keep popup window within padding of window size
        if(size.w > win.w.i-50){size.w = win.w.i-50;}
        if(size.h > win.h.i-50){size.h = win.h.i-50;}
    
        // do math
        const x = win.w.o / 2 + win.x - (size.w / 2);
        const y = win.h.o / 2 + win.y - (size.h / 2);
        return window.open(url, title, `width=${size.w},height=${size.h},left=${x},top=${y}${options}`);
    }
    

    usage:

    // width and height are optional (defaults: width = '75%' height = '16:9')
    popupWindow('https://www.google.com', 'Title', '75%', '16:9', {/* options (optional) */});
    
    // options can be an object, array, or string
    
    // example: object (only in object, true/false get replaced with 'yes'/'no')
    const options = {scrollbars: false, resizable: true};
    
    // example: array
    const options = ['scrollbars=no', 'resizable=yes'];
    
    // example: string (same as window.open() string)
    const options = 'scrollbars=no,resizable=yes';
    

提交回复
热议问题