Center a popup window on screen?

前端 未结 18 1035
一整个雨季
一整个雨季 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条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-11-22 17:41

    This hybrid solution worked for me, both on single and dual screen setup

    function popupCenter (url, title, w, h) {
        // Fixes dual-screen position                              Most browsers      Firefox
        const dualScreenLeft = window.screenLeft !== undefined ? window.screenLeft : window.screenX;
        const dualScreenTop = window.screenTop !== undefined ? window.screenTop : window.screenY;
        const left = (window.screen.width/2)-(w/2) + dualScreenLeft;
        const top = (window.screen.height/2)-(h/2) + dualScreenTop;
        return window.open(url, title, 'toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=no, resizable=no, copyhistory=no, width='+w+', height='+h+', top='+top+', left='+left);
    }
    

提交回复
热议问题