Center a popup window on screen?

前端 未结 18 767
一整个雨季
一整个雨季 2020-11-22 16:51

How can we center a popup window opened via javascript window.open function on the center of screen variable to the currently selected screen resolution ?

相关标签:
18条回答
  • 2020-11-22 17:18

    You can use css to do it, just give the following properties to the element to be placed in the center of the popup

    element{
    
    position:fixed;
    left: 50%;
    top: 50%;
    -ms-transform: translate(-50%,-50%);
    -moz-transform:translate(-50%,-50%);
    -webkit-transform: translate(-50%,-50%);
     transform: translate(-50%,-50%);
    
    }
    
    0 讨论(0)
  • 2020-11-22 17:19

    Facebook use the following algorithm to position their login popup window:

    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;
    }
    
    0 讨论(0)
  • 2020-11-22 17:19

    My version with ES6 JavaScript.
    Works well on Chrome and Chromium with dual screen setup.

    function openCenteredWindow({url, width, height}) {
        const pos = {
            x: (screen.width / 2) - (width / 2),
            y: (screen.height/2) - (height / 2)
        };
    
        const features = `width=${width} height=${height} left=${pos.x} top=${pos.y}`;
    
        return window.open(url, '_blank', features);
    }
    

    Example

    openCenteredWindow({
        url: 'https://stackoverflow.com/', 
        width: 500, 
        height: 600
    }).focus();
    
    0 讨论(0)
  • 2020-11-22 17:20
    function fnPopUpWindow(pageId) {
         popupwindow("hellowWorld.php?id="+pageId, "printViewer", "500", "300");
    }
    
    function popupwindow(url, title, w, h) {
        var left = Math.round((screen.width/2)-(w/2));
        var top = Math.round((screen.height/2)-(h/2));
        return window.open(url, title, 'toolbar=no, location=no, directories=no, status=no, '
                + 'menubar=no, scrollbars=yes, resizable=no, copyhistory=no, width=' + w 
                + ', height=' + h + ', top=' + top + ', left=' + left);
    }
    
    <a href="javascript:void(0);" onclick="fnPopUpWindow('10');">Print Me</a>
    

    Note: you have to use Math.round for getting the exact integer of width and height.

    0 讨论(0)
  • 2020-11-22 17:21

    If you want to center it on the frame you are currently in, I would recommend this function:

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

    Similar to Crazy Tim's answer, but doesn't use window.top. This way, it will work even if the window is embedded in an iframe from a different domain.

    0 讨论(0)
  • 2020-11-22 17:27

    It works very well in Firefox.
    Just change the top variable to any other name and try again

            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);
    
    0 讨论(0)
提交回复
热议问题