在屏幕上居中显示一个弹出窗口? [英] Center a popup window on screen?

查看:148
本文介绍了在屏幕上居中显示一个弹出窗口?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何通过屏幕变量中心的javascript window.open 功能打开弹出窗口到当前所选屏幕分辨率?

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 ?

推荐答案

单/双监视器功能(归功于 http://www.xtf.dk - 谢谢!)

SINGLE/DUAL MONITOR FUNCTION (credit to http://www.xtf.dk - thank you!)

更新:它也适用于Windows由于@Frost,现在不会超出屏幕的宽度和高度!

如果你在双显示器上,窗口将水平居中,但不是垂直...使用此函数来解释它。

If you're on dual monitor, the window will center horizontally, but not vertically... use this function to account for that.

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

用法示例:

PopupCenter('http://www.xtf.dk','xtf','900','500');  

CREDIT转到: http://www.xtf.dk/2011/08/center-new-popup-window-even- on.html (我想链接到这个页面但是为了防止这个网站出现故障,代码就在这里,欢呼!)

CREDIT GOES TO: http://www.xtf.dk/2011/08/center-new-popup-window-even-on.html (I wanted to just link out to this page but just in case this website goes down the code is here on SO, cheers!)

这篇关于在屏幕上居中显示一个弹出窗口?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆