码迷,mamicode.com
首页 > Web开发 > 详细

用JS来控制 div的高度随浏览器变化而变化

时间:2016-10-22 09:40:30      阅读:271      评论:0      收藏:0      [点我收藏+]

标签:order   bsp   窗口   函数   span   变化   client   nbsp   blog   

<div id="test" style=" border: solid 1px #f00; "></div>
<script type="text/javascript">
<!--
//作者:凌陈亮www.lingchenliang.com(QQ:57404811)
autodivheight();
function autodivheight(){ //函数:获取尺寸
    //获取浏览器窗口高度
    var winHeight=0;
    if (window.innerHeight)
        winHeight = window.innerHeight;
    else if ((document.body) && (document.body.clientHeight))
        winHeight = document.body.clientHeight;
    //通过深入Document内部对body进行检测,获取浏览器窗口高度
    if (document.documentElement && document.documentElement.clientHeight)
        winHeight = document.documentElement.clientHeight;
    //DIV高度为浏览器窗口的高度
    //document.getElementById("test").style.height= winHeight +"px";
    //DIV高度为浏览器窗口高度的一半
    document.getElementById("test").style.height= winHeight/2 +"px";
}
window.onresize=autodivheight; //浏览器窗口发生变化时同时变化DIV高度
//-->
</script>

 

用JS来控制 div的高度随浏览器变化而变化

标签:order   bsp   窗口   函数   span   变化   client   nbsp   blog   

原文地址:http://www.cnblogs.com/hanqishihu/p/5986685.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!