Changing div height according screen resolution javascript

﹥>﹥吖頭↗ 提交于 2019-12-10 19:14:43

问题


Here is the problem. I have

<div id="main"></div>

I want to check user resolution and change his height according user resolution, using javascript?

Javascript:

window.onload = function() {
    var height = screen.height
    document.getElementById(main).style.height = height;
};

I even try this:

window.onload = function() { 
    var height = screen.height 
    var ele = document.getElementById(main);
    if(ele.style.height == "auto") 
    {
            ele.style.height = height;
    }
    else {
        ele.style.height = height;
    }
}; 

回答1:


If you want set the Screen Height:

var mainNode = document.getElementById("main");
mainNode.style.height = screen.height + "px";

Screen Avail Height

Screen Height

DOMElement Client Height

Screen height is different from client height (document.documentElement.clientHeight).



来源:https://stackoverflow.com/questions/12855116/changing-div-height-according-screen-resolution-javascript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!