Window.open 居中

function   openNewwin(strURL,intWidth,intHeight){  
      var   posX;  
      var   posY;  
      var   porps;  
      var   wWidth   =   600;  
      var   wHeight   =   400;  

      if(strURL   ==   ""){  
          alert("很抱歉,您没有输入要弹出窗口的URL值!!")  
        }  
      else  
      {  
          if(intWidth   ==   null   ||   intWidth   ==   0   ||   intWidth   ==   "")  
          {  
              posX   =   Math.round((screen.width   -   wWidth)/2);  
          }  
          else  
              {    
                  wWidth   =   intWidth  
                  posX   =   Math.round((screen.width   -   parseInt(intWidth))/2);  
              }  
          if(intHeight   ==   null   ||   intHeight   ==   0   ||   intHeight   ==   "")  
              posY   =   Math.round((screen.height   -   wHeight)/2);  
          else  
              {  
                  posY   =   Math.round((screen.height   -   parseInt(intHeight))/2);  
                  wHeight   =   intHeight  
              }  
              porps   =   "scrollbars=1,resizable=1,munubar=0,toolbar=0,location=0,left="   +   posX   +",top="   +   posY   +   ",width="   +   wWidth   +   ",height="   +   wHeight;  
              window.open(strURL,"_blank",porps)  
        }  
}

### 实现 `window.open` 弹出窗口居中的方法 为了使 `window.open` 方法弹出的浏览器窗口能够居中显示,可以通过计算当前屏幕尺寸以及浏览器窗口的位置来动态设置新窗口的 `left` 和 `top` 属性。以下是具体的实现方式: #### 计算窗口位置的核心逻辑 1. **获取屏幕宽度和高度**:通过 `screen.width` 和 `screen.height` 获取整个屏幕的分辨率。 2. **获取可用区域宽度和高度**:通过 `screen.availWidth` 和 `screen.availHeight` 获取除去任务栏后的可用屏幕大小。 3. **获取当前窗口相对于屏幕的位置**:使用 `window.screenX` 或 `window.screenLeft` 来获取浏览器窗口左上角距离屏幕左侧的距离;使用 `window.screenY` 或 `window.screenTop` 来获取顶部距离[^3]。 4. **定义新窗口的宽度和高度**:设定要弹出的新窗口的具体尺寸。 基于以上数据,可以计算新窗口应该放置在屏幕上的具体坐标,使其居中显示。 #### JavaScript代码示例 以下是一个完整的函数用于创建一个居中的弹窗: ```javascript function openCenteredWindow(url, width, height) { // 设置窗口的宽高 var iWidth = width; var iHeight = height; // 获得窗口的垂直位置 var iTop = (window.screenY || window.screenTop || 0) + (screen.height - iHeight) / 2; // 获得窗口的水平位置 var iLeft = (window.screenX || window.screenLeft || 0) + (screen.width - iWidth) / 2; // 如果需要考虑多屏环境下的调整 if (typeof screen.left !== 'undefined') { iLeft += screen.left; // 多屏环境下可能需要额外偏移量 } // 打开新的窗口并传递参数 window.open( url, '_blank', 'width=' + iWidth + ',height=' + iHeight + ',top=' + iTop + ',left=' + iLeft + ',toolbar=0,menubar=no,scrollbars=yes,resizable=yes,location=no,status=no' ); } ``` 调用此函数时传入目标 URL、期望的宽度和高度即可打开一个居中的窗口。例如: ```javascript openCenteredWindow('http://example.com', 800, 600); ``` #### 特殊场景处理——双屏支持 对于双屏或多屏环境中可能出现的问题,上述代码已经考虑到部分情况。如果发现某些特定设备或配置下仍然存在问题,则需进一步检测主屏与副屏之间的相对关系,并适当调整 `iLeft` 的值以适应不同显示器布局[^2]。 --- ### 注意事项 - 不同浏览器可能会对 `window.open()` 行为有所限制,尤其是在涉及广告拦截器的情况下。因此建议测试多种主流浏览器的行为差异。 - 用户操作习惯可能导致手动拖动原生窗口改变其初始定位规则,在实际应用前应充分验证效果一致性[^4]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值