html - CSS transition on height auto 仅在添加类时有效,在删除类时无效

标签 html css sass

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

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

但是当打开和关闭高度时,它只会在添加类时转换。删除类(将 height 改回 0 并将 max-height 改回 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;
  }
 }

最佳答案

这是高度,你在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;
  }
 }

关于html - CSS transition on height auto 仅在添加类时有效,在删除类时无效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52482441/

相关文章:

javascript - 如何创建垂直导航菜单?

html - 在导航栏内垂直对齐所有内容

html - 带对齐的水平 div

html - float 和溢出-x : hidden;

sass - compass 为我提供了通往 Sprite 的错误路径

css - 转换错误:Jekyll::Converters::Scss 遇到错误

php - 如何知道用户何时关闭浏览器?聊天应用

javascript - 编辑 HTML 表格行数据

html - 使用 compass-watch 将单个 Scss 文件转换为 CSS

javascript - Chrome 未捕获语法错误 : Unexpected token <