求助:根据屏幕宽度改变div的宽度js怎么写? 求助:根据屏幕宽度改变div的宽度js怎么写?哪位大侠能帮我?感激不尽,谢谢! 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 下面是从我的网页程序中取出的片段。我是根据浏览器当前宽度设置 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'); // 动态创建 divmyDiv.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; 我想先判断屏幕的宽度,然后再给图片添加一个宽度,比如屏幕是640px;那么我给图片的宽度就是600px;如果屏幕宽度是320px;那么给图片宽度就是300px; 1.求屏幕宽度2.改变div宽度http://www.cnblogs.com/dolphinX/archive/2012/11/19/2777756.html 跳转页面能否用post方式传递变量? 在 ext form 动态添加一个 ComboBox ★★★★如何获取网页上activex的handle 有没有这样的日期控件(不是下拉的)? JS写俄罗斯方块完美注释版 JavaScript中用jQuery的$("#categoryPanel > ul").tabs();问题??? 获取xml 中的CDate中的内容。 求肋播放器插件的问题 ********JavaScript中add是关键字吗?为什么我的input出错? 关于导向新页的问题 在指定位置显示层,当窗口大小变化时,层的位置就偏移了,有什么办法解决该问题? 有关仿人人回复框的写法
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 其他属性。
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;
2.改变div宽度http://www.cnblogs.com/dolphinX/archive/2012/11/19/2777756.html