求助:根据屏幕宽度改变div的宽度js怎么写?哪位大侠能帮我?感激不尽,谢谢!

解决方案 »

  1.   

    下面是从我的网页程序中取出的片段。我是根据浏览器当前宽度设置 div 宽度,这样更实用些。如果要取屏幕宽度,可以改用 window.screen.width。var winWidth = document.body.scrollWidth - 12;      // 取窗口宽度
    var winHeight = document.body.scrollHeight - 24;    // 取窗口高度
    if (winHeight <= 0) winHeight = 640;   // 初始高度并不总能取出,设默认值var myDiv = document.createElement('div');   // 动态创建 div
    myDiv.style.width = winWidth + 'px';        // 设置宽度
    myDiv.style.height = winHeight + 'px';      // 设置高度以下设置 div 其他属性。
      

  2.   

    你把DIV的宽度写成相对宽度,屏幕宽度变了,DIV的也会变
      

  3.   

    div 默认是没有宽度的。设置为百分比有些情况下可以,有些应用不希望 div 随着窗口改变自动撑开,这样就不行。对移动应用来说,情况会更复杂。
      

  4.   

    楼上几位估计理解错了,我估计楼主需要的是根据屏幕随时调整div大小的方法。
    autoHeight()
    {
      var $width = window.screen.width;
      $("#div").css("width",$width);
    }
    window.onresize = autoHeight;
    其中window.onresize这方法捕捉窗口宽度变化的。不过这东西有个问题,就是IE6会无限循环调用,如果不用IE6就没问题。先确定你用不用IE6,用的话再找别的方法。
      

  5.   

    设置百分比获取可见区域宽度:
     function  getClientWidth()

       return (navigator.userAgent.toLowerCase().indexOf('opera') != -1)?document.body.clientWidth:document.documentElement.clientWidth;  
    };
      

  6.   

    --调整页面高度---
    function refreshSize(){
    var totolHeight =  document.body.clientHeight;
    var height = totolHeight - 564;
    console.log(totolHeight + ' - ' + height);
    document.getElementById('carFormDiv').style.height = height+'px';
    };
    refreshSize();
    window.onresize = refreshSize;
      

  7.   

    我想先判断屏幕的宽度,然后再给图片添加一个宽度,比如屏幕是640px;那么我给图片的宽度就是600px;如果屏幕宽度是320px;那么给图片宽度就是300px;
      

  8.   

    1.求屏幕宽度
    2.改变div宽度http://www.cnblogs.com/dolphinX/archive/2012/11/19/2777756.html