自动调整高度CSS

Auto resize height CSS

本文关键字:CSS 高度 调整      更新时间:2023-09-26

我有这样的html代码

<div id="wrapper">
  <div id="collapse"></div>
  <div id="content"></div>
</div>

CSS代码

#wrapper {
  width:100%;
  height: 100%;
}
#collapse {
  width:100%;
  height: 30%;
}
#content {
  width:100%;
  height: 70%;
}

我想问的是如何使#content的高度适合#wrapper时,#collapse是隐藏的(假设有一个js脚本,使#collapse显示和隐藏)?

试试这个:
HTML代码:

<div id="main">
<div id="Example">Hello</div>
<button id="Toggle">Toggle</button>
<div id="body">This is Body!!!</div>
</div>  
JS代码:
$('#Toggle').on('click', function() {
    $('#Example').slideToggle({
        duration: 1000,
        easing: 'easeOutCubic'
    });
});  
CSS代码:
#Example {
    height: 100px;
    background: #cc0000;
}
#main{ height:500px;
background-color:yellow;}
#body{  height: 100%;
background-color:blue;}  

您可以删除按钮并自定义此代码。
请看这个DEMO

您可以使用jQuery完成此操作。

See the example 
[http://jsfiddle.net/atinder123/6hq8h/][1]

  [1]: http://jsfiddle.net/atinder123/6hq8h/

在同一个JS中隐藏你添加的collapse元素

document.getElementById('content').style.height = '100%';

当然,当你再次显示折叠时,也要反过来