通過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>

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM