通过JS获取屏幕高度,借助屏幕高度设置div的高度

HTML:

<div class="content" style="background: #e07f7f;"></div>

JS:

<script>
  // 获取屏幕高度
  autoDivSize();
      function  autoDivSize(){ //函数:获取尺寸
        // 获取窗口宽度
        if (window.innerWidth){
            winWidth = window.innerWidth;
            console.log(winWidth+','+"oneW");
        }
        else if ((document.body) && (document.body.clientWidth)){
            winWidth = document.body.clientWidth;
            console.log(winWidth+','+"twoW");
        }
        // 获取窗口高度
        if (window.innerHeight){
            winHeight = window.innerHeight;
            console.log(winHeight+','+"oneH");
        }
        else if ((document.body) && (document.body.clientHeight)){
            winHeight = document.body.clientHeight;
            console.log(winHeight+','+"oneH");
        }
         // 通过深入 Document 内部对 body 进行检测,获取窗口大小
        if (document.documentElement && document.documentElement.clientHeight &&       document.documentElement.clientWidth)
        {
            winHeight = document.documentElement.clientHeight;
            winWidth = document.documentElement.clientWidth;
            console.log(winHeight+','+winWidth);
        }
        //DIV高度为浏览器窗口高度 的60%
        document.getElementById("content").style.height= winHeight * 1 +"px";
        console.log(document.getElementById("content").style.height);
    }
    window.οnresize=autoDivSize; //浏览器窗口发生变化时同时变化DIV高度
</script>

 

posted @ 2020-04-01 13:24  wqll  阅读(1418)  评论(0编辑  收藏  举报