我正在尝试设置未指定高度的div
的高度。我不能像许多答案那样max-height
,因为有多个身高量。
我尝试将transition: height 0.2s ease
添加到div
,但这并没有帮助。如何将动画添加到高度?我非常喜欢css,但如果不可能,如果我完全不得不使用JavaScript,我就会这样做。
.tabGroup {
background-color: brown;
color: yellowgreen;
transition: height 0.2s ease;
}
.tabGroup > div {
display: none;
}
.tab1:checked ~ .tab1,
.tab2:checked ~ .tab2,
.tab3:checked ~ .tab3 {
display: block;
}

<div class="tabGroup">
<input type="radio" name="tabGroup1" id="rad1" class="tab1" checked="checked" />
<label for="rad1">Tab 1</label>
<input type="radio" name="tabGroup1" id="rad2" class="tab2" />
<label for="rad2">Tab 2</label>
<input type="radio" name="tabGroup1" id="rad3" class="tab3" />
<label for="rad3">Tab 3</label>
<div class="tab1">
Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has
survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing
software like Aldus PageMaker including versions of Lorem Ipsum.
</div>
<div class="tab2">
Tab 2 content
</div>
<div class="tab3">
Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature from 45 BC, making it over 2000 years old. Richard McClintock, a Latin professor at Hampden-Sydney College in Virginia, looked up
one of the more obscure Latin words, consectetur, from a Lorem Ipsum passage, and going through the cites of the word in classical literature, discovered the undoubtable source. Lorem Ipsum comes from sections 1.10.32 and 1.10.33 of "de Finibus Bonorum
et Malorum" (The Extremes of Good and Evil) by Cicero, written in 45 BC. This book is a treatise on the theory of ethics, very popular during the Renaissance. The first line of Lorem Ipsum, "Lorem ipsum dolor sit amet..", comes from a line in section
1.10.32. The standard chunk of Lorem Ipsum used since the 1500s is reproduced below for those interested. Sections 1.10.32 and 1.10.33 from "de Finibus Bonorum et Malorum" by Cicero are also reproduced in their exact original form, accompanied by
English versions from the 1914 translation by H. Rackham.
</div>
</div>
&#13;
答案 0 :(得分:1)
我不能像许多答案那样做max-height,因为有多个身高量。
max-height
技巧是将其设置为任何内容永远不会达到的值。这样所有div高度都可以转换到它们的内容高度。所以最后如果有多个高度数量并不重要,只有max-height
值设置在最高值之上。
这是我的尝试,但并不完美,但过渡就在那里。
.tabGroup > div {
max-height:0px;
transition:max-height 0.5s;
overflow:hidden;
}
.tab1:checked ~ .tab1,
.tab2:checked ~ .tab2,
.tab3:checked ~ .tab3 {
max-height:500px;
}
答案 1 :(得分:1)
扩展@ EricGuan的答案。您可以使用具有绝对位置的元素,并仅在选择其中一个选项时应用转换。
请在此处查看解决方案:http://jsfiddle.net/9m526ezL/2/
.tabGroup {
background-color: brown;
color: yellowgreen;
position: relative; // have the container with relative
}
.tabGroup > div {
// each tab with absolute position and with painted background.
position: absolute;
background-color: brown;
color: yellowgreen;
width: 100%;
max-height: 0;
overflow: hidden;
}
// Little suggestion, use ID's for your menu instead of classes,
// they should each be unique anyway.
#rad1:checked ~ .tab1,
#rad2:checked ~ .tab2,
#rad3:checked ~ .tab3 {
transition: max-height 0.5s;
max-height: 600px;
}
编辑: 这是另一个版本:http://jsfiddle.net/9m526ezL/4/
一个简单的技巧就是让上一个文字消失,就是当标签处于非活动状态时,使color
透明或与背景颜色相同。
答案 2 :(得分:0)
我知道如果你不需要,你真的不想使用js,但我认为你可以用一些jQuery轻松完成这个。
这样的事情:
$(document).ready(function(){
$("#rad1").click(function(){
$("#tab1").slideDown("slow");
});
});