场景:
- 用户有两个显示器。
- 他们的浏览器在次要显示器上打开。
- 他们在浏览器中单击一个链接,该链接调用window.open()函数并指定了特定的窗口偏移量。
- 弹出窗口总是在他们的主要显示器上打开。
在JavaScript中是否有方法可以使弹出窗口在与初始浏览器窗口(启动器)相同的显示器上打开?
场景:
在JavaScript中是否有方法可以使弹出窗口在与初始浏览器窗口(启动器)相同的显示器上打开?
function getMouseXY( e ) {
if ( event.clientX ) { // Grab the x-y pos.s if browser is IE.
CurrentLeft = event.clientX + document.body.scrollLeft;
CurrentTop = event.clientY + document.body.scrollTop;
}
else { // Grab the x-y pos.s if browser isn't IE.
CurrentLeft = e.pageX;
CurrentTop = e.pageY;
}
if ( CurrentLeft < 0 ) { CurrentLeft = 0; };
if ( CurrentTop < 0 ) { CurrentTop = 0; };
return true;
}
以下是我从 Facebook OAuth API 中不怕丢脸地逆向工程出来的东西。在 Firefox/Chrome 的主屏和副屏上进行了测试。
function popup_params(width, height) {
var a = typeof window.screenX != 'undefined' ? window.screenX : window.screenLeft;
var i = typeof window.screenY != 'undefined' ? window.screenY : window.screenTop;
var g = typeof window.outerWidth!='undefined' ? window.outerWidth : document.documentElement.clientWidth;
var f = typeof window.outerHeight != 'undefined' ? window.outerHeight: (document.documentElement.clientHeight - 22);
var h = (a < 0) ? window.screen.width + a : a;
var left = parseInt(h + ((g - width) / 2), 10);
var top = parseInt(i + ((f-height) / 2.5), 10);
return 'width=' + width + ',height=' + height + ',left=' + left + ',top=' + top + ',scrollbars=1';
}
window.open(url, "window name", "location=1,toolbar=0," + popup_params(modal_width, modal_height));
var h = (a < 0) ? window.screen.width + a : a;
使用这行代码后,我的屏幕定位不正确。 - plainjimbo// Pops a window relative to the current window position
function popup(url, winName, xOffset, yOffset) {
var x = (window.screenX || window.screenLeft || 0) + (xOffset || 0);
var y = (window.screenY || window.screenTop || 0) + (yOffset || 0);
return window.open(url, winName, 'top=' +y+ ',left=' +x))
}
按照以下方式调用,它将在当前窗口的顶部打开
popup('http://www.google.com', 'my-win');
popup('http://www.google.com', 'my-win', 30, 30);
/**
* Finds the screen element for the monitor that the browser window is currently in.
* This is required because window.screen is the screen that the page was originally
* loaded in. This method works even after the window has been moved across monitors.
*
* @param {function} cb The function that will be called (asynchronously) once the screen
* object has been discovered. It will be passed a single argument, the screen object.
*/
function getScreenProps (cb) {
if (!window.frames.testiframe) {
var iframeEl = document.createElement('iframe');
iframeEl.name = 'testiframe';
iframeEl.src = "about:blank";
iframeEl.id = 'iframe-test'
document.body.appendChild(iframeEl);
}
// Callback when the iframe finishes reloading, it will have the
// correct screen object
document.getElementById('iframe-test').onload = function() {
cb( window.frames.testiframe.screen );
delete document.getElementById('iframe-test').onload;
};
// reload the iframe so that the screen object is reloaded
window.frames.testiframe.location.reload();
};
如果您希望始终在窗口所在的任何监视器的左上角打开窗口,则可以使用以下方法:
function openAtTopLeftOfSameMonitor(url, winName) {
getScreenProps(function(scr){
window.open(url, winName, 'top=0,left=' + scr.left);
})
}
在当前屏幕上打开居中窗口,同时与Chrome浏览器兼容:
function popupOnCurrentScreenCenter(url, title, w, h) {
var dualScreenLeft = typeof window.screenLeft !== "undefined" ? window.screenLeft : screen.left;
var dualScreenTop = typeof window.screenTop !== "undefined" ? window.screenTop : screen.top;
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 left = ((width / 2) - (w / 2)) + dualScreenLeft;
var top = ((height / 2) - (h / 2)) + dualScreenTop;
var newWindow =
window.open(url, title, 'scrollbars=yes, width=' + w + ', height=' + h + ', top=' + top + ', left=' + left);
// Puts focus on the newWindow
if (window.focus) {
newWindow.focus();
}
}
只有用户11153的版本适用于Chrome和双屏幕。这是它的TypeScript版本。
popupOnCurrentScreenCenter(url: string, title: string, w: number, h: number): Window|null {
var dualScreenLeft = typeof window.screenLeft !== "undefined" ? window.screenLeft : (<any>screen).left;
var dualScreenTop = typeof window.screenTop !== "undefined" ? window.screenTop : (<any>screen).top;
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 left = ((width / 2) - (w / 2)) + dualScreenLeft;
var top = ((height / 2) - (h / 2)) + dualScreenTop;
var newWindow =
window.open(url, title, 'scrollbars=yes, width=' + w + ', height=' + h + ', top=' + top + ', left=' + left);
// Puts focus on the newWindow
if (window.focus && newWindow) {
newWindow.focus();
}
return newWindow;
}
我最近遇到了这个问题,最终找到了一种方法来定位弹出窗口在屏幕上的位置。请查看我的解决方案,链接在我的github页面上:https://github.com/svignara/windowPopUp
关键在于使用window.screen
对象,该对象返回availWidth
、availHeight
、availLeft
和availTop
值(以及width
和height
)。有关对象中变量的完整列表以及这些变量表示的内容,请参见https://developer.mozilla.org/en-US/docs/DOM/window.screen。
本质上,我的解决方案是在单击弹出窗口的触发器时查找window.screen
的值。这样我就知道它是从哪个监视器屏幕上单击的。而availLeft
值则处理其余部分。以下是具体步骤:
offsetLeft = availableLeft + ( (availableWidth - modalWidth) / 2 )
如果您知道每个显示器的分辨率,您可以估计这个问题。
相对于鼠标(如上所述)或原始浏览器窗口的位置也可能很有用,尽管您必须假设用户使用浏览器最大化(这并不一定是真实情况)。
var x = 0;
var y = 0;
var myWin = window.open(''+self.location,'mywin','left='+x+',top='+y+',width=500,height=500,toolbar=1,resizable=0');