【发布时间】:2020-07-03 09:16:57
【问题描述】:
我正在尝试使用 javascript 将弹出窗口居中,但它仅水平居中,而不是垂直居中。
垂直卡在屏幕顶部。我尝试调整“top”变量,但无论我将其更改为什么,它都停留在屏幕顶部。
string url = "LoginPage.aspx";
StringBuilder sb = new StringBuilder();
sb.Append("<script type = 'text/javascript'> var height=500/((window.top.innerWidth ? window.top.innerWidth : document.documentElement.clientWidth ? document.documentElement.clientWidth : screen.width) / window.top.screen.availWidth); var width=550/((window.top.innerWidth ? window.top.innerWidth : document.documentElement.clientWidth ? document.documentElement.clientWidth : screen.width) / window.top.screen.availWidth);");
sb.Append("var left = ((window.top.innerWidth ? window.top.innerWidth : document.documentElement.clientWidth ? document.documentElement.clientWidth : screen.width) - 550) / 2 / ((window.top.innerWidth ? window.top.innerWidth : document.documentElement.clientWidth ? document.documentElement.clientWidth : screen.width) / window.top.screen.availWidth) + (window.top.screenLeft !== undefined ? window.top.screenLeft : window.top.screenX);");
sb.Append("var top = ((window.top.innerHeight ? window.top.innerHeight : document.documentElement.clientHeight ? document.documentElement.clientHeight : screen.height) - 500) / 2 / ((window.top.innerWidth ? window.top.innerWidth : document.documentElement.clientWidth ? document.documentElement.clientWidth : screen.width) / window.top.screen.availWidth) + (window.top.screenTop !== undefined ? window.top.screenTop : window.top.screenY);");
sb.Append("window.open('");
sb.Append(url);
sb.Append("','_blank','toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=no, resizable=no, copyhistory=no,");
sb.Append("width='+width+',height='+height+',top='+top+',left='+left);");
sb.Append("</script>");
ClientScript.RegisterStartupScript(this.GetType(), "loginscript", sb.ToString());
我的解决方案来自:Center a popup window on screen?
【问题讨论】:
-
在第 5 行(关于高度)有很多“top.innerHeight”,但在该行的后面都是“top.innerWidth”。难道这些都必须是“top.innerHeight”吗?
-
@SirExotic 我明白你为什么会这样说,但那些在这篇文章中指的是 SystemZoom:stackoverflow.com/questions/4068373/… 而且,当我尝试将顶部变量更改为其他固定值时,该位置获胜不改变。所以我的猜测是错误不在变量声明中。
标签: javascript java html css popup