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(我想链接到这个页面,但以防这个网站发生故障代码在这里,欢呼!)
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)
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)
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)
我的建议是使用剩余空间的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
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)
归档时间: |
|
查看次数: |
460112 次 |
最近记录: |