Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

transition height 从 0 到 auto 无效应对方法 #51

Open
ChuChencheng opened this issue Jun 23, 2024 · 0 comments
Open

transition height 从 0 到 auto 无效应对方法 #51

ChuChencheng opened this issue Jun 23, 2024 · 0 comments
Labels

Comments

@ChuChencheng
Copy link
Owner

背景

如下 CSS 要实现一个容器从高度 0 正常展开到 auto(或反之)是无效的

<div>
contents
</div>
div {
  transition: height .2s;
}

div.active {
  height: 0;
}

解决

使用 grid 布局,把 transition 属性设置为 grid-template-rows,并从 0fr 变化到 1fr 可解决,但需要多套一层 div

<div class="wrapper">
  <div class="inner">
    contents
  </div>
</div>
.wrapper {
  display: grid;
  transition: grid-template-rows .2s;
  grid-template-rows: 0fr;
}

.wrapper.active {
  grid-template-rows: 1fr;
}

.inner {
  overflow: hidden;
}

References

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant