【问题标题】:Popup centering horizontally but not vertically弹出水平居中但不垂直居中
【发布时间】: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


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 2016-07-18
    • 1970-01-01
    • 2019-07-19
    • 2015-08-29
    相关资源
    最近更新 更多