单/双显示器功能(归功于http://www.xtf.dk http://www.xtf.dk- 谢谢你!)
更新:由于@Frost,它也适用于现在未达到屏幕宽度和高度的最大窗口!
如果您使用双显示器,窗口将水平居中,但不会垂直居中...使用此功能来解决这一问题。
const 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 width = window.innerWidth ? window.innerWidth : document.documentElement.clientWidth ? document.documentElement.clientWidth : screen.width;
const height = window.innerHeight ? window.innerHeight : document.documentElement.clientHeight ? document.documentElement.clientHeight : screen.height;
const systemZoom = width / window.screen.availWidth;
const left = (width - w) / 2 / systemZoom + dualScreenLeft
const top = (height - h) / 2 / systemZoom + dualScreenTop
const newWindow = window.open(url, title,
`
scrollbars=yes,
width=${w / systemZoom},
height=${h / systemZoom},
top=${top},
left=${left}
`
)
if (window.focus) newWindow.focus();
}
使用示例:
popupCenter({url: 'http://www.xtf.dk', title: 'xtf', w: 900, h: 500});
信用归:http://www.xtf.dk/2011/08/center-new-popup-window-even-on.html http://www.xtf.dk/2011/08/center-new-popup-window-even-on.html(我只想链接到此页面,但以防万一此网站出现故障,代码位于此处。)