在屏幕上居中显示一个弹出窗口?

Zo *_*Has 264 javascript

我们如何将通过javascript window.open函数打开的弹出窗口置于屏幕变量的中心到当前所选的屏幕分辨率?

Ton*_*y M 437

单/双监控功能(信用到http://www.xtf.dk - 谢谢!)

更新:由于@Frost,它也适用于没有达到屏幕宽度和高度的窗户!

如果你在双显示器上,窗口将水平居中,但不是垂直居中...使用此功能来解决这个问题.

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();
}
Run Code Online (Sandbox Code Playgroud)

用法示例:

PopupCenter('http://www.xtf.dk','xtf','900','500');  
Run Code Online (Sandbox Code Playgroud)

信用证转到:http://www.xtf.dk/2011/08/center-new-popup-window-even-on.html(我想链接到这个页面,但以防这个网站发生故障代码在这里,欢呼!)

  • 这是一个很好的解决方案,应该得到更多的认可 (40认同)
  • 原始问题发布于2010年,原始解决方案发布于2010年.我对2013年发布的双监视器原始解决方案的评论,2013年发布的双监视器答案.您对2015年三监视器的评论.您现在需要回答2015年的三重监控解决方案.按照这个速度,我们将在2020年为5台显示器,2025年的6台显示器,2030年的7台显示器提供答案......让我们继续这个循环吧! (13认同)
  • 使用全局变量(宽度/高度),哎哟! (10认同)
  • 感谢您的荣誉,我现在让我的示例在最小化的窗口上工作:http://www.xtf.dk/2011/08/center-new-popup-window-even-on.html (8认同)
  • 经过一番游戏后,这并不像我想象的那样有效.更简单,例外的答案效果更好.这仅在启动页面最大化时才有效. (7认同)
  • @TonyM我已经更新了答案.是的循环需要继续! (2认同)

oez*_*ezi 324

试试这样:

function popupwindow(url, title, w, h) {
  var left = (screen.width/2)-(w/2);
  var top = (screen.height/2)-(h/2);
  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);
} 
Run Code Online (Sandbox Code Playgroud)

  • 此功能不适用于双显示器设置.我在下面发布了一个单一和双显示器解决方案. (17认同)
  • 不以我的第二台显示器为中心(从主显示器向上).双屏幕的答案也失败了. (2认同)
  • 如果您希望将窗口置于浏览器中间而不是屏幕中间(例如,用户将其浏览器调整为一半大小),则无法使用此功能.要在浏览器中居中,请使用window.innerWidth&window.innerHeight替换screen.width和screen.height (2认同)

Cra*_*Tim 42

由于在多显示器设置中确定当前屏幕中心的复杂性,更容易的选择是将弹出窗口置于父窗口的中心.只需将父窗口作为另一个参数传递:

function popupWindow(url, title, win, w, h) {
    const y = win.top.outerHeight / 2 + win.top.screenY - ( h / 2);
    const x = win.top.outerWidth / 2 + win.top.screenX - ( w / 2);
    return win.open(url, title, 'toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=no, resizable=no, copyhistory=no, width='+w+', height='+h+', top='+y+', left='+x);
}
Run Code Online (Sandbox Code Playgroud)

  • 这对我在双屏上非常有效。即使在移动或调整窗口大小时,它也表示从其打开的窗口的中心。这应该是公认的答案。谢谢。 (2认同)
  • 我同意@OliB——这非常有效,解决了我们最近遇到的开发问题!应该是 2019 年公认的答案。 (2认同)

Omn*_*ity 21

资料来源:http://www.nigraphic.com/blog/java-script/how-open-new-window-popup-center-screen

function PopupCenter(pageURL, title,w,h) {
  var left = (screen.width/2)-(w/2);
  var top = (screen.height/2)-(h/2);
  var targetWin = window.open (pageURL, 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);
  return targetWin;
} 
Run Code Online (Sandbox Code Playgroud)


Tau*_*hts 14

如果你想把它放在你当前所在的框架上,我推荐这个功能:

function popupwindow(url, title, w, h) {
    var y = window.outerHeight / 2 + window.screenY - ( h / 2)
    var x = window.outerWidth / 2 + window.screenX - ( w / 2)
    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=' + y + ', left=' + x);
} 
Run Code Online (Sandbox Code Playgroud)

类似于Crazy Tim的回答,但不使用window.top.这样,即使窗口嵌入来自不同域的iframe,它也会起作用.


小智 13

它在Firefox中运行良好.
只需将顶部变量更改为任何其他名称,然后重试

        var w = 200;
        var h = 200;
        var left = Number((screen.width/2)-(w/2));
        var tops = Number((screen.height/2)-(h/2));

window.open("templates/sales/index.php?go=new_sale", '', 'toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=no, resizable=no, copyhistory=no, width='+w+', height='+h+', top='+tops+', left='+left);
Run Code Online (Sandbox Code Playgroud)

  • 完全没必要做`Number(...)`. (5认同)

mr.*_*123 6

我的建议是使用剩余空间的33%或25%的顶级位置,
而不是这里发布的其他示例的50%,
主要是因为窗口标题,
看起来更好,更舒适的用户,

完整代码:

    <script language="javascript" type="text/javascript">
        function OpenPopupCenter(pageURL, title, w, h) {
            var left = (screen.width - w) / 2;
            var top = (screen.height - h) / 4;  // for 25% - devide by 4  |  for 33% - devide by 3
            var targetWin = window.open(pageURL, 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);
        } 
    </script>
</head>
<body>
    <button onclick="OpenPopupCenter('http://www.google.com', 'TEST!?', 800, 600);">click on me</button>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)



看看这一行:
var top =(screen.height - h)/ 4; //为25% - 偏离4 | 为33% - 偏离3


Rol*_*oós 6

Facebook 使用以下算法来定位他们的登录弹出窗口:

function PopupCenter(url, title, w, h) {
  var userAgent = navigator.userAgent,
      mobile = function() {
        return /\b(iPhone|iP[ao]d)/.test(userAgent) ||
          /\b(iP[ao]d)/.test(userAgent) ||
          /Android/i.test(userAgent) ||
          /Mobile/i.test(userAgent);
      },
      screenX = typeof window.screenX != 'undefined' ? window.screenX : window.screenLeft,
      screenY = typeof window.screenY != 'undefined' ? window.screenY : window.screenTop,
      outerWidth = typeof window.outerWidth != 'undefined' ? window.outerWidth : document.documentElement.clientWidth,
      outerHeight = typeof window.outerHeight != 'undefined' ? window.outerHeight : document.documentElement.clientHeight - 22,
      targetWidth = mobile() ? null : w,
      targetHeight = mobile() ? null : h,
      V = screenX < 0 ? window.screen.width + screenX : screenX,
      left = parseInt(V + (outerWidth - targetWidth) / 2, 10),
      right = parseInt(screenY + (outerHeight - targetHeight) / 2.5, 10),
      features = [];
  if (targetWidth !== null) {
    features.push('width=' + targetWidth);
  }
  if (targetHeight !== null) {
    features.push('height=' + targetHeight);
  }
  features.push('left=' + left);
  features.push('top=' + right);
  features.push('scrollbars=1');

  var newWindow = window.open(url, title, features.join(','));

  if (window.focus) {
    newWindow.focus();
  }

  return newWindow;
}
Run Code Online (Sandbox Code Playgroud)