Animate Height using CSS

2019-05-22 13:56发布

I am trying to animate the height of a div that wasn't specified a height. I can't do max-height like many answers suggest because there are multiple height amounts.

I tried adding transition: height 0.2s ease to the div, but that didn't help. How can I add animation to the height? I strongly prefer css, but if that's not possible, if I completely have to use JavaScript, I'll do it.

JSFiddle

.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>

3条回答
姐就是有狂的资本
2楼-- · 2019-05-22 14:03

I can't do max-height like many answers suggest because there are multiple height amounts.

The max-height trick is to set it at a value that will never be reached by any content. This way all the div heights can transition to their content height. So in the end it doesn't matter if there are multiple height amounts, only that the max-height value is set above the highest one.

Here's my attempt, it's not perfect but the transition is there.

.tabGroup > div {
    max-height:0px;
    transition:max-height 0.5s;
    overflow:hidden;
}

.tab1:checked ~ .tab1,
.tab2:checked ~ .tab2,
.tab3:checked ~ .tab3 {
    max-height:500px;
}

http://jsfiddle.net/guanzo/hgr0cd0q/4/

查看更多
手持菜刀,她持情操
3楼-- · 2019-05-22 14:10

Expanding on @EricGuan's answer. You could have the elements with absolute position and only apply the transition when one of the options is selected.

See a solution here: 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;
}

EDIT: Here is another version: http://jsfiddle.net/9m526ezL/4/

A simple trick to make it seems as if the previous text is gone is to make the color transparent or the same color as the background when the tab is not active.

查看更多
萌系小妹纸
4楼-- · 2019-05-22 14:19

I know you don't really want to use js if you don't have to, BUT I think you could easily accomplish this with some jQuery.

Something like this:

$(document).ready(function(){
    $("#rad1").click(function(){
        $("#tab1").slideDown("slow");
    });
});
查看更多
登录 后发表回答