所以,这似乎很容易在我的头脑中做,但我无法让它工作,谷歌没有帮助我,你能看到我做错了吗?
我有5个div,我希望它们与浏览器窗口的高度相同。即使是调整大小。
这是我的代码:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<script type="text/javascript">
function setDivHeight(){
var windowHeight = window.innerHeight;
if(windowHeight > 400){
document.getElementsByClassName("minHeight").style.backgroundColor = 'black';
}
else if(windowHeight < 400){
document.getElementsByClassName("minHeight").style.backgroundColor = 'red';
}
}
window.addEventListener("resize",setDivHeight,false);
</script>
</head>
<style type="text/css">
body{margin:0; padding:0;}
.minHeight{
height:20px;
}
.a{
background: #09C;
}
.b{
background: #09b;
}
.c{
background: #09d;
}
.d{
background: #09e;
}
.e{
background: #09f;
}
</style>
<body onload="setDivHeight()">
<div class="a minHeight"></div>
<div class="b minHeight"></div>
<div class="c minHeight"></div>
<div class="d minHeight"></div>
<div class="e minHeight"></div>
</body>
</html>
答案 0 :(得分:2)
您不需要使用javascript。查看JSFiddle DEMO HERE
检查以下代码
<强> HTML 强>
<html xmlns="http://www.w3.org/1999/xhtml">
<body>
<div class="a minHeight"></div>
<div class="b minHeight"></div>
<div class="c minHeight"></div>
<div class="d minHeight"></div>
<div class="e minHeight"></div>
</body>
</html>
<强> CSS 强>
html{height:100%}
body{margin:0; padding:0;height:100%;}
.minHeight{
height:100%;
width: 40px;
}
.a{
background-color: #09C;
}
.b{
background-color: #09b;
}
.c{
background-color: #09d;
}
.d{
background-color: #09e;
}
.e{
background-color: #09f;
}
div{float: left;}
答案 1 :(得分:1)
所以你想要div
与屏幕大小相同吗?
position: absolute; top: 0px; bottom: 0px; left: 0px; right: 0px;