自动调整高度CSS

时间:2013-01-18 05:59:10

标签: javascript html css

我有像这样的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%;
}

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

3 个答案:

答案 0 :(得分:3)

试试这个:
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

答案 1 :(得分:0)

你可以用jQuery做到这一点。

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


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

答案 2 :(得分:0)

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

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

当你再次显示崩溃时,当然是反过来

相关问题