发表评论
<div id="test" style=" border: solid 1px #f00; "></div> <script type="text/javascript"> 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("test").style.height= winHeight * 0.6 +"px"; console.log(document.getElementById("test").style.height); } window.onresize=autoDivSize; //浏览器窗口发生变化时同时变化DIV高度 </script>