设置相对于“window.innerHeight”的高度无效
Setting height relative to `window.innerHeight` not working
对于一些人来说,这可能是一个非常愚蠢的问题,但我肯定无法理解这背后的问题。代码相当简单,如下所示:
HTML:
<!DOCTYPE html>
<html lang="en-US">
<head>
<title>####</title>
<meta charset="UTF-8">
<link rel="stylesheet" type="text/css" href="css.css">
</head>
<body onload="resizeDiv();">
<div id="testElement">ABCD</div>
<div id="secElement">ABC</div>
<script type="text/javascript" src="javascript.js"></script>
</body>
</html>
CSS:
html , body{
height:100%;
}
#testElement{
background-color:black;
color:white;
}
#secElement{
font-size:50px;
}
JS:
(function immediate(){
document.getElementById('secElement').textContent = window.innerHeight;
}());
function resizeDiv(){
document.getElementById('testElement').style.height = (window.innerHeight - 50) * 0.9;
}
现在,通过此代码,具有id
"testElement"的div
的高度应该是window.innerHeight - 20
的90%。但是,似乎什么都不起作用。请帮忙。
添加"px":
document.getElementById('testElement').style.height = (window.innerHeight - 50) * 0.9 + "px";
body
标记有一个分配给resizeDiv()
的onload
处理程序,但您的脚本位于页面底部,因此onload
处理程序无法引用它。
使用document.getElementById('testElement').setAttribute("style","height:" + (window.innerHeight - 50) * 0.9) + ' px';
您可以在此处找到使用它的更多解释:在JavaScript 中设置div宽度和高度
相关文章:
- electronic BrowserWindow的最小高度和宽度在hide()show()方法之后不起作用
- ExtJS——在展开/折叠时调整面板高度
- 分析高度属性时出现意外值{{specs.height}}.index.html
- 单击页面上的链接后高度发生变化
- 如果我在javascript中输入无效的电子邮件或空白,如何显示特定的文本框边框红色
- 不加载宽度和高度的角度pintura
- jquery代码在Mozilla中有效,但在其他浏览器上无效
- 查找元素高度,包括边距
- 如何自动调整标签的高度以适应内容
- Javascript高度问题
- 使用JavaScript根据窗口/视口的高度动态调整图像大小
- 如何计算每个元素's的高度,并将这些值用作函数中的变量
- 如果宽度是百分比,如何设置以px为单位的图像高度
- 这个问号在Flow中意味着什么:“;?()=>“无效”;
- 从字符串末尾删除空白无效
- 通过jquery设置最小高度
- Jqx图表给出错误错误:
属性高度=“-1”的负值无效 - 设置最大高度无效
- flot异常flot高度=0的维度无效
- 设置相对于“window.innerHeight”的高度无效