我们如何将通过屏幕变量中心的javascript window.open
函数打开的弹出窗口居中放置到当前选定的屏幕分辨率?在屏幕上居中显示一个弹出窗口?
回答
单/双头显示功能(信贷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 : screen.left;
var dualScreenTop = 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();
}
}
用法示例:
PopupCenter('http://www.xtf.dk','xtf','900','500');
幸得:http://www.xtf.dk/2011/08/center-new-popup-window-even-on.html(我想只是链接了该网页,但以防万一,本网站宕机的代码是在这里对SO,欢呼!)
尝试这样的:
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);
}
来源: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;
}
这不适用于Chrome – Adrian 2012-06-20 09:48:23
不适用于FF – 2012-08-10 23:05:38
它的工作原理相当不错,在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);
执行'Number(...)'完全没有必要。 – gustavohenke 2013-11-13 18:49:31
我的建议是使用顶部位置33%或25%的剩余空间,
,而不是50%作为其他例子贴在这里,
01:主要是窗口标题,
哪个更好看,更舒适的用户,
完整的代码,因为
<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>
退房这一行:
VAR顶部=(screen.height - H)/ 4; // 25% - 由4 |分开33% - devide 3
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>
注意:您必须使用Math.round
来获取宽度和高度的确切整数。
您可以使用CSS来做到这一点,只是给下列属性的元素放置在弹出
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%);
}
中心由于确定在当前屏幕中心的复杂性多监视器设置,更简单的选项是将弹出窗口居中置于父窗口之上。只要父窗口传递的另一个参数:
function popupwindow(url, title, win, w, h) {
var y = win.top.outerHeight/2 + win.top.screenY - (h/2)
var 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);
}
这似乎是Facebook用于分享按钮弹出窗口的技术。 – 2016-02-25 14:43:26
如果要居中您目前在框架上,我会推荐这个功能:
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);
}
类似疯狂添的回答,但不使用window.top。这样,即使窗口嵌入在来自不同域的iframe中,它也可以工作。
这里是上述方案的替代版本...
function openPopupCenter(url, title, w, h) {
// Fixes dual-screen position
// Most browsers use window.screenLeft
// Firefox uses screen.left
var dualScreenLeft = getFirstNumber(window.screenLeft, screen.left),
dualScreenTop = getFirstNumber(window.screenTop, screen.top),
width = getFirstNumber(window.innerWidth, document.documentElement.clientWidth, screen.width),
height = getFirstNumber(window.innerHeight, document.documentElement.clientHeight, screen.height),
left = ((width/2) - (w/2)) + dualScreenLeft,
top = ((height/2) - (h/2)) + dualScreenTop,
newWindow = window.open(url, title, getSpecs());
// Puts focus on the newWindow
if (window.focus) {
newWindow.focus();
}
return newWindow;
function getSpecs() {
return 'scrollbars=yes, width=' + w + ', height=' + h + ', top=' + top + ', left=' + left;
}
function getFirstNumber() {
for(var i = 0, len = arguments.length; i < len; i++) {
var value = arguments[i];
if (typeof value === 'number') {
return value;
}
}
}
}
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;
}
- 1. 弹出窗口需要居中在屏幕上
- 2. 弹出窗口显示在屏幕中心和上下文外
- 3. 在锁定屏幕上显示弹出窗口
- 4. 如何在Windows登录屏幕上弹出一个窗口
- 5. 在屏幕上居中显示面板
- 6. 一次显示一个弹出窗口
- 7. 弹出式窗口,中央屏幕
- 8. 居中javascript弹出窗口
- 9. 如何使用JavaScript显示全屏幕弹出窗口?
- 10. 所有屏幕上的显示窗口
- 11. 在父窗口所在的屏幕上显示子窗口
- 12. 显示一个模态弹出窗口
- 13. 如何显示其他弹出窗口上的弹出窗口?
- 14. 关于在小屏幕上显示Bootstrap弹出窗口的问题
- 15. 在主窗口运行之前显示一个弹出窗口
- 16. jQuery对话框弹出窗口显示在同一个窗口
- 17. 如何使一个弹出窗口中的URL显示在父窗口上?
- 18. 制作一个窗口弹出和跳出屏幕边缘
- 19. 有一个弹出窗口居中居中父 - Glade,GTK,Python
- 20. 弹出窗口破坏屏幕方向
- 21. 让一个链接在弹出窗口中显示一个div
- 22. 在GNU屏幕的hardstatus中显示一个窗口的用户
- 23. 如何在屏幕上触摸的地方上方显示一个弹出窗口
- 24. 在同一个弹出窗口上显示消息
- 25. 在小屏幕上居中弹出溢出问题
- 26. 如何在浏览器中显示弹出窗口时冻结屏幕?
- 27. 在屏幕顶部显示wpf窗口
- 28. 如何在页面上居中显示一个模态窗口?
- 29. 弹出窗口不显示
- 30. 弹出窗口不显示
这是一个很好的解决方案,应该得到一些更多的认可 – Jim 2013-06-05 23:53:41
经过一番玩,这不像我想的那样好。更简单的例外答案效果更好。这仅适用于启动页面最大化的情况。 – Bart 2013-09-19 20:02:24
感谢您的信用,我已经使我的示例工作在最小化窗口现在:http://www.xtf.dk/2011/08/center-new-popup-window-even-on.html – Frost 2013-11-05 11:42:56