简体   繁体   English

根据窗户内部高度更改Div高度

[英]Change Div height according windows inner height

I need a little help here. 我需要一点帮助。

I want to change the height of div according to the screen inner height for which i have written a java script which is as follows 我想根据我编写的Java脚本的屏幕内部高度来更改div的高度,如下所示

<script>
function height()
{
var h = window.innerHeight;

document.getElementById("bg").style.height = h; 
}
</script>

Whenever i use 每当我使用

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

i doesn't write anything to the div element #bg but when i define a height in px like 500px i does write height in div element #bg. 我没有向div元素#bg写任何东西,但是当我以px为单位定义高度(如500px)时,我的确在div元素#bg中写了高度。

You'll need to add 'px' to the height value: 您需要在高度值上添加“ px”:

function height()
{
var h = window.innerHeight;

document.getElementById("bg").style.height = h+'px'; 
}

And obviously call the function at some point 并且显然在某个时候调用该函数

height();

Check out the fiddle - http://jsfiddle.net/MRttV/ 查看小提琴-http: //jsfiddle.net/MRttV/

You can use pure css to do this I guess. 我猜您可以使用纯CSS来做到这一点。 by defining: 通过定义:

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

Or you can also use the "vh" value. 或者,您也可以使用“ vh”值。 But it doesn't work on old browsers 但它不适用于旧的浏览器

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM