弹出窗口水平居中,但不垂直居中



我正在尝试使用 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());

我的解决方案取自:在屏幕上居中弹出窗口?

我找到了解决方案。问题是变量名称"top"。我不知道为什么,但 top 返回了一个窗口对象。我将变量名称更改为"topp",这奏效了。我不知道为什么,但如果有人知道我很好奇!

最新更新