Javascript 控制从JS到css的div高度?
我的站点()上有此脚本: 函数ResizeIFrame(){ var div=document.getElementById(“包装器”); var myWidth=0,myHeight=0; if(typeof(window.innerWidth)=‘number’){ //非IE myWidth=window.innerWidth; myHeight=window.innerHeight-80; }else if(document.documentElement&(document.documentElement.clientWidth | | document.documentElement.clientHeight)){ //IE 6+处于“标准兼容模式” myWidth=document.documentElement.clientWidth; myHeight=document.documentElement.clientHeight-80; }else if(document.body&(document.body.clientWidth | | document.body.clientHeight)){ //IE4兼容 myWidth=document.body.clientWidth; myHeight=document.body.clientHeight-80; } div.style.width=myWidth+'px'; div.style.height=myHeight+'px'; } window.onmouseclick=window.onmouseover=window.onload=window.onresize=ResizeIFrame; setTimeout(“ResizeIFrame()”,5); setTimeout(“ResizeIFrame()”,25); setTimeout(“ResizeIFrame()”,50); setTimeout(“ResizeIFrame()”,100); setTimeout(“ResizeIFrame()”,1000); 它用于控制“包装器”div的高度,以便将其调整为客户机窗口高度-X(变量编号) 我想知道如何在css上翻译JS,或者如果工作正常的话,如何在表上翻译JSJavascript 控制从JS到css的div高度?,javascript,css,Javascript,Css,我的站点()上有此脚本: 函数ResizeIFrame(){ var div=document.getElementById(“包装器”); var myWidth=0,myHeight=0; if(typeof(window.innerWidth)=‘number’){ //非IE myWidth=window.innerWidth; myHeight=window.innerHeight-80; }else if(document.documentElement&(document.docu
#iframe_id {
height:100%
}
是仅使用css可以获得的最接近的。因为你甚至不能用纯css做简单的数学运算(你可以用sass,但我离题了)
prolly是最直接的JS方式,不包括jQuery/some library,你可以这样做,但看看你的网站,你需要某种形式的“粘性页脚”解决方案,这并不完全是琐碎的。下面的网站提到了一个解决方案,也许你可以使用html+css
编辑:哦,在这个解决方案中,您可能需要更改主内容的溢出,因为您希望它调整大小,而不是滚动。这是一篇相当古老的文章,但这也是我面临的一个问题,最终能够通过拼凑来自多个源的代码来解决。。。以下是最终对我有效的解决方案:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Dynamic Div Resizing With Plain Javascript</title>
</head>
<body style="margin: 0 auto;">
<div id="page-wrapper">
<div id="header" style="min-height: 32px; background: #aaa;">
header
</div>
<div id="content" style="background: #ddd;">
content
</div>
<div id="footer" style="min-height: 32px; background: #aaa;">
footer
</div>
</div>
<script type="text/javascript">
function autorun()
{
var divHeightContent = window.innerHeight,
divHeightHeader = document.getElementById('header').offsetHeight,
divHeightFooter = document.getElementById('footer').offsetHeight;
document.getElementById('content').style.height=( divHeightContent - divHeightHeader - divHeightFooter + 'px' );
}
if (window.addEventListener) window.addEventListener("load", autorun, false);
else if (window.attachEvent) window.attachEvent("onload", autorun);
else window.onload = autorun;
window.onresize = autorun;
</script>
</body>
</html>
使用普通Javascript动态调整Div大小
标题
内容
页脚
函数自动运行()
{
var divHeightContent=window.innerHeight,
divHeightHeader=document.getElementById('header')。offsetHeight,
divHeightFooter=document.getElementById('footer')。offsetHeight;
document.getElementById('content').style.height=(divHeightContent-divHeightHeader-divHeightFooter+'px');
}
if(window.addEventListener)window.addEventListener(“加载”,自动运行,false);
else if(window.attachEvent)window.attachEvent(“onload”,自动运行);
else window.onload=自动运行;
window.onresize=自动运行;
谢谢!!是的,那个粘脚我想这才是真正的问题,我会努力的!,是的,我的css已经有了非常具体的溢出控件,我希望它们不会改变太多!酷!,很久以前,我甚至想不起这个问题了,谢谢!
function set_height_to_parent(id, offset){
var e = document.getElementById(id);
e.height = parentElement.offsetHeight-offset;
}
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Dynamic Div Resizing With Plain Javascript</title>
</head>
<body style="margin: 0 auto;">
<div id="page-wrapper">
<div id="header" style="min-height: 32px; background: #aaa;">
header
</div>
<div id="content" style="background: #ddd;">
content
</div>
<div id="footer" style="min-height: 32px; background: #aaa;">
footer
</div>
</div>
<script type="text/javascript">
function autorun()
{
var divHeightContent = window.innerHeight,
divHeightHeader = document.getElementById('header').offsetHeight,
divHeightFooter = document.getElementById('footer').offsetHeight;
document.getElementById('content').style.height=( divHeightContent - divHeightHeader - divHeightFooter + 'px' );
}
if (window.addEventListener) window.addEventListener("load", autorun, false);
else if (window.attachEvent) window.attachEvent("onload", autorun);
else window.onload = autorun;
window.onresize = autorun;
</script>
</body>
</html>