【问题标题】:CSS transition on height auto only working when adding class, not when removing class高度自动上的 CSS 过渡仅在添加类时有效,而不是在删除类时
【发布时间】:2018-09-24 15:15:26
【问题描述】:

目标是在 div 的高度上使用 CSS 过渡,从 0 更改为 auto 高度,以便它可以作为视觉对象滑动打开和关闭。

因为 CSS height: auto 不能有过渡,所以我使用 max-height 作为过渡。当我添加“扩展”类时,高度是自动的......

但是在打开和关闭高度时,它仅在添加类时才会转换。删除类(将高度更改回 0 并将最大高度更改回 0)过渡不存在并且是即时的

.information{
  height: 0;
  max-height: 0;
  width: 100%;
  overflow: hidden;
  z-index: -1;
  background: #434C69;
  transition: max-height 700ms ease-in-out;


  &.expanded{
    height: auto;
    max-height: 500px;
    border-bottom: 1px solid $secondary-blue;
  }
 }

【问题讨论】:

  • 您如何管理切换?尽管我相信这是预期的行为。

标签: html css sass


【解决方案1】:

这是height,您在max-height 上添加transition,但是当您删除扩展类时,您的height 立即更改为0

您可以在.information 类上设置height: auto;,并仅在max-height 上进行转换。

.information{
  height: auto;
  max-height: 0;
  width: 100%;
  overflow: hidden;
  z-index: -1;
  background: #434C69;
  transition: max-height 700ms ease-in-out;


  &.expanded{
    max-height: 500px;
    border-bottom: 1px solid $secondary-blue;
  }
 }

【讨论】:

  • 比我快 :D,你也可以在第一个删除 height:0 并让第二个自动
  • 甚至更简单:两个类都不需要指定高度
  • 这是jsfiddle.net/u2qwLjeo 的小提琴。此处不需要最大高度,并且过渡到高度
  • @MattOpen,你假设他想要一个预定义的高度。我认为他想使用 height: auto;因为这样他就可以让内容决定容器的高度。
【解决方案2】:

你应该创建一个类来反转信息类的影响并分配它而不是删除信息类。 希望对你有用。

【讨论】:

    猜你喜欢
    • 2013-05-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-14
    • 2023-04-05
    • 2017-03-28
    • 1970-01-01
    相关资源
    最近更新 更多