根据窗口内部高度改变Div高度

Change Div height according windows inner height

本文关键字:高度 改变 Div 内部 窗口      更新时间:2023-09-26

我需要一点帮助。

我想根据屏幕内部高度改变div的高度为此我编写了一个Java脚本,如下所示

<script>
function height()
{
var h = window.innerHeight;
document.getElementById("bg").style.height = h; 
}
</script>

每当我使用

document.getElementById("bg").style.height = h; 

i不向div元素#bg写入任何内容但是当我在px中定义高度时,比如500px,我会在div元素#bg中写入高度

需要在height值中添加" px ":

function height()
{
var h = window.innerHeight;
document.getElementById("bg").style.height = h+'px'; 
}

显然在某个点调用函数

height();

查看小提琴- http://jsfiddle.net/MRttV/

我猜你可以使用纯css来做到这一点。通过定义:

   body,
     html {
     height: 100%;
   }
   #bg {
     height: 100%;
     width: 100%; /* or the value you want if element is empty */
   }

或者您也可以使用"vh"值。但是在旧的浏览器上不能用