繁体   English   中英

如何让 JavaScript 在当前监视器上打开一个弹出窗口

[英]How do I get JavaScript to open a popup window on the current monitor

设想:

  1. 用户有两个监视器。
  2. 他们的浏览器在辅助监视器上打开。
  3. 他们单击浏览器中的链接,该链接调用具有特定顶部和左侧窗口偏移量的 window.open()。
  4. 弹出窗口始终在其主监视器上打开。

JavaScript 中有什么方法可以让弹出窗口在与初始浏览器窗口(打开器)相同的监视器上打开?

您不能指定监视器,但可以将弹出窗口的位置指定为相对于单击导致窗口弹出的位置。

使用 getMouseXY() 函数获取要作为 left 和 top args 传递给 window.open() 方法的值。 (left 和 top args 仅适用于 V3 及更高版本的浏览器)。

window.open 文档: http ://www.javascripter.net/faq/openinga.htm

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));
// 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);

关键是 window.screenX/screenLeft 为您提供与整个桌面相关的位置,而不仅仅是显示器。

window.screen.left 将是为您提供所需信息的理想人选。 问题是它是在页面加载时设置的,用户可以将窗口移动到另一个监视器。

更多研究

这个问题的最终解决方案(不仅仅是从当前窗口位置偏移)需要知道窗口所在屏幕的尺寸。由于屏幕对象不会随着用户移动窗口而更新,我们需要制作我们的自己检测当前屏幕分辨率的方法。 这是我想出的

/**
 * 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();
    }
}

我最近遇到了这个问题,终于找到了一种将弹出窗口定位在触发它的屏幕上的方法。 在我的 github 页面上查看我的解决方案: https ://github.com/svignara/windowPopUp

诀窍在于使用window.screen对象,该对象返回availWidthavailHeightavailLeftavailTop值(以及widthheight )。 有关对象中变量的完整列表以及这些变量所代表的含义,请查看https://developer.mozilla.org/en-US/docs/DOM/window.screen

本质上,只要单击弹出窗口的触发器,我的解决方案就会找到window.screen的值。 这样我就可以确定从哪个监视器屏幕上单击它。 availLeft值负责其余部分。 就是这样:

基本上,如果左侧的第一个可用像素 ( availLeft ) 为负数,则表明“主”监视器左侧有一个监视器。 同样,如果左起第一个可用像素大于 0,则意味着以下两种情况之一:

  1. 监视器位于“主”监视器的右侧,或者
  2. 屏幕左侧有一些“垃圾”(可能是应用程序停靠栏或 Windows 开始菜单)

无论哪种情况,您都希望弹出窗口的偏移量从左侧可用像素之后开始。

offsetLeft = availableLeft + ( (availableWidth - modalWidth) / 2 )

只有 user11153 的版本适用于 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;
}

如果你知道每台显示器的分辨率,你可以估计这个。 对于公共网站来说这是一个坏主意,但如果您知道(出于某种奇怪的原因)这种情况将始终适用,则可能会有用。

相对于鼠标(如上所述)或相对于原始浏览器窗口的位置也可能很有用,尽管您必须假设用户使用最大化的浏览器(这不一定是真的)。

只要您知道落在特定显示器上的 x 和 y 位置,您就可以执行以下操作:

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');

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM