简体   繁体   English

React组件的简单过渡

[英]Simple transition for React component

I have a React component that I show/hide based on a button toggle from within its parent component. 我有一个React组件,我根据其父组件内部的按钮切换来显示/隐藏。 Instead of just appearing/disappearing on the page, I would like to animate the mounting and unmounting of the component to make it look as if it was sliding down from, and back into the parent. 我不只是要在页面上显示/消失,还希望对组件的安装和卸载进行动画处理,以使其看起来像是从父级滑落并滑回父级。 The parent is always visible. 父母总是可见的。 在此处输入图片说明

One important note is that there are also components within the first child. 一个重要的注意事项是,第一个孩子中还包含一些组件。 One of my attempts involving CSS transitions have resulted in these children being stuck in place when the first child slides up/down. 我涉及CSS过渡的尝试之一导致这些孩子在第一个孩子向上/向下滑动时被卡在适当的位置。 Additionally, Child has no fixed height -- it could be 100px or 1000px. 此外,Child没有固定的高度-可以是100px或1000px。

This is my only animation in the application, so I don't really want to get anything too heavy to drive it. 这是我在应用程序中唯一的动画,所以我真的不想增加任何东西来驱动它。 I'm struggling to find what other people are using for this kind of thing. 我正在努力寻找其他人正在使用这种东西。

For that kind of simple animations I generally use CSSTransation . 对于那种简单的动画,我通常使用CSSTransation The idea is pretty simple, this component will attach some classes to your element depending on the component state (mounting, unmounting,...), and you have to provide css for the animation. 这个想法很简单,该组件将根据组件状态(安装,卸载等)将一些类附加到您的元素上,并且您必须为动画提供css。

I finally solved this with a small package called react-animate-height . 我终于用一个名为react-animate-height的小程序包解决了这个问题。

<div>
  <AnimateHeight
    duration={ 500 }
    height={ height } 
  >
    <Child />
  </AnimateHeight>
</div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM