首页 > 解决方案 > 弹出水平居中但不垂直居中

问题描述

我正在尝试使用 javascript 将弹出窗口居中,但它仅水平居中,而不是垂直居中。

垂直它卡在屏幕顶部。我尝试调整“顶部”变量,但无论我将其更改为什么,它都停留在屏幕顶部。

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());

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

标签: javascriptjavahtmlcsspopup

解决方案


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


推荐阅读