我们如何将通过javascript window.open
函数打开的弹出窗口居中于屏幕变量中心的当前所选屏幕分辨率的中心?
SINGLE/DUAL MONITOR FUNCTION (credit to http://www.xtf.dk - thank you!)
(单/双显示器功能 (感谢http ://www.xtf.dk-谢谢!))
UPDATE: It will also work on windows that aren't maxed out to the screen's width and height now thanks to @Frost!
(更新:由于@Frost,它现在也可以在尚未超出屏幕宽度和高度的窗口上运行!)
If you're on dual monitor, the window will center horizontally, but not vertically... use this function to account for that.
(如果您使用的是双显示器,则窗口将水平居中,而不是垂直居中...使用此功能可以解决此问题。)
function PopupCenter(url, title, w, h) {
// Fixes dual-screen position Most browsers Firefox
var dualScreenLeft = window.screenLeft != undefined ? window.screenLeft : window.screenX;
var dualScreenTop = window.screenTop != undefined ? window.screenTop : window.screenY;
var width = window.innerWidth ? window.innerWidth : document.documentElement.clientWidth ? document.documentElement.clientWidth : screen.width;
var height = window.innerHeight ? window.innerHeight : document.documentElement.clientHeight ? document.documentElement.clientHeight : screen.height;
var systemZoom = width / window.screen.availWidth;
var left = (width - w) / 2 / systemZoom + dualScreenLeft
var top = (height - h) / 2 / systemZoom + dualScreenTop
var newWindow = window.open(url, title, 'scrollbars=yes, width=' + w / systemZoom + ', height=' + h / systemZoom + ', top=' + top + ', left=' + left);
// Puts focus on the newWindow
if (window.focus) newWindow.focus();
}
Usage Example:
(用法示例:)
PopupCenter('http://www.xtf.dk','xtf','900','500');
CREDIT GOES TO: http://www.xtf.dk/2011/08/center-new-popup-window-even-on.html (I wanted to just link out to this page but just in case this website goes down the code is here on SO, cheers!)
(信用转到: http : //www.xtf.dk/2011/08/center-new-popup-window-even-on.html (我想直接链接到此页面,但以防万一该网站出现故障代码就在这里,加油!))