javascript - 使用 CSS 制作高度动画

标签 javascript css animation

我正在尝试为未指定高度的 div 的高度设置动画。我无法像许多答案所建议的那样执行 max-height ,因为存在多个高度值。

我尝试将 transition: height 0.2s escape 添加到 div,但这没有帮助。如何为高度添加动画?我非常喜欢 css,但如果那不可能,如果我完全必须使用 JavaScript,我会这样做。

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>

最佳答案

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

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;
}

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

关于javascript - 使用 CSS 制作高度动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35882403/

相关文章:

javascript - 主干 'this' 被设置为 'window'

javascript - React 示例网页的控制台错误 - 有状态组件

javascript - 一些挑剔的 HTML、CSS 和 JS 问题

javascript - CSS代码/文件可以加载JS代码/文件吗?

javascript - 如何在Angular表单中连续设置两个输入字段

windows-phone-7 - WP7 的键盘进入和退出动画的长度是多少?

javascript - 谷歌折线图正在绘制额外的线条

html - 在带有 link_to 的 ERB 中生成的 HREF 不可点击

android - 场景。在 View 下滑动过渡

javascript - 如何同时添加和删除类?