gpt4 book ai didi

javascript - 浏览器窗口居中问题

转载 作者:行者123 更新时间:2023-11-28 02:57:23 26 4
gpt4 key购买 nike

我正在尝试使用以下代码通过表单按钮启动居中的浏览器窗口...

http://www.bbc.co.uk','testwin','width=400,height=400,left=(screen.availWidth-400)/2,top=(screen.availHeight-400)/2');返回错误>测试按钮

新浏览器显示正确的高度和宽度,但忽略 left 和 top 属性,因此窗口显示在左上角。

我是否过于雄心勃勃地试图使这段代码内嵌?我是否应该使用任何其他语法,或者我应该放弃并调用函数(如果可以避免的话,我宁愿不这样做)?

我使用的是 Firefox 3.0.17(最新),但在 IE7 中也会出现同样的效果。

TIA,艾伦·哈里斯-里德

最佳答案

http://www.tek-tips.com/faqs.cfm?fid=2296解释了如何做。但我建议使用 float Div,因为有弹出窗口拦截器。

假设您要打开一个尺寸为 200h x 150w 的窗口。通过取高度和宽度的一半(分别为 100 和 75),您可以精确定位窗口的中心。然后,通过一些数学运算,您就可以将窗口居中。这是如何完成的

<SCRIPT LANGUAGE="JavaScript">
function MyPopUpWin() {
var iMyWidth;
var iMyHeight;
//half the screen width minus half the new window width (plus 5 pixel borders).
iMyWidth = (window.screen.width/2) - (75 + 10);
//half the screen height minus half the new window height (plus title and status bars).
iMyHeight = (window.screen.height/2) - (100 + 50);
//Open the window.
var win2 = window.open("filename.htm","Window2","status=no,height=200,width=150,resizable=yes,left=" + iMyWidth + ",top=" + iMyHeight + ",screenX=" + iMyWidth + ",screenY=" + iMyHeight + ",toolbar=no,menubar=no,scrollbars=no,location=no,directories=no");
win2.focus();
}
</SCRIPT>

要在 HTML 中调用此函数,请使用:

<A HREF="javascript:MyPopUpWin()">This is the link</a>

关于javascript - 浏览器窗口居中问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2256326/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com