弹出窗口水平居中但不是垂直居中
Popup centering horizontally but not vertically
我试图用 javascript 将弹出窗口 window 居中,但它只是水平居中,而不是垂直居中。
它在垂直方向上卡在屏幕顶部。我尝试调整“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?
我找到了解决方案。问题是变量名“top”。我不知道为什么,但 top 返回了一个 window 对象。我将变量名称更改为“topp”,并且有效。我不知道为什么,但如果有人知道我很好奇!
我试图用 javascript 将弹出窗口 window 居中,但它只是水平居中,而不是垂直居中。
它在垂直方向上卡在屏幕顶部。我尝试调整“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?
我找到了解决方案。问题是变量名“top”。我不知道为什么,但 top 返回了一个 window 对象。我将变量名称更改为“topp”,并且有效。我不知道为什么,但如果有人知道我很好奇!