JavaScript 根据窗口大小自动调整div。

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了JavaScript 根据窗口大小自动调整div。相关的知识,希望对你有一定的参考价值。

function getObj(name){
    if (document.getElementById){
        this.obj = document.getElementById(name);
        this.style = document.getElementById(name).style;
    }
    else if (document.all){
        this.obj = document.all[name];
        this.style = document.all[name].style;
    }
}

function getWinSize(){
   var iWidth = 0, iHeight = 0;

   if (document.documentElement && document.documentElement.clientHeight){
       iWidth = parseInt(window.innerWidth,10);
       iHeight = parseInt(window.innerHeight,10);
   }
   else if (document.body){
       iWidth = parseInt(document.body.offsetWidth,10);
       iHeight = parseInt(document.body.offsetHeight,10);
   }

   return {width:iWidth, height:iHeight};
}

function resize_id(obj) {                           
    var oContent = new getObj(obj);
    var oWinSize = getWinSize();
    if ((oWinSize.height - parseInt(oContent.obj.offsetTop,10))>0)
        oContent.style.height = (oWinSize.height - parseInt(oContent.obj.offsetTop,10));
}

window.onresize = function() { resize_id('ID_of_Container'); }

以上是关于JavaScript 根据窗口大小自动调整div。的主要内容,如果未能解决你的问题,请参考以下文章

根据寡妇大小调整内容 div 的大小,其余元素将保持不变

jQuery动画通过div调整大小保持自动定位

使用css自动调整中心div的大小?

根据内容大小动画和调整 div 动态高度

使用窗口调整大小缩放固定位置 div 的宽度

如何根据文本的长度自动调整固定 DIV 中的文本大小?