日期:2014-05-16  浏览次数:20324 次

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

------解决方案--------------------
下面是从我的网页程序中取出的片段。我是根据浏览器当前宽度设置 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 其他属性。

------解决方案--------------------
你把DIV的宽度写成相对宽度,屏幕宽度变了,DIV的也会变
------解决方案--------------------
设置为百分比就行了
------解决方案--------------------
div 默认是没有宽度的。设置为百分比有些情况下可以,有些应用不希望 div 随着窗口改变自动撑开,这样就不行。对移动应用来说,情况会更复杂。

------解决方案--------------------
楼上几位估计理解错了,我估计楼主需要的是根据屏幕随时调整div大小的方法。


autoHeight()
{
  var $width = window.screen.width;
  $("#div").css("width",$width);
}
window.onresize = autoHeight;


其中window.onresize这方法捕捉窗口宽度变化的。不过这东西有个问题,就是IE6会无限循环调用,如果不用IE6就没问题。先确定你用不用IE6,用的话再找别的方法。
------解决方案--------------------
设置百分比

获取可见区域宽度:
 function  getClientWidth()

   return (navigator.userAgent.toLowerCase().indexOf('opera') != -1)?document.body.clientWidth:document.documentElement.clientWidth;  
};
------解决方案--------------------
--调整页面高度---
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;
------解决方案--------------------
1.求屏幕宽度
2.改变div宽度

http://www.cnblogs.com/dolphinX/archive/2012/11/19/2777756.html