我正在尝试使通知出现/消失,但是 transitionAppear 不起作用。我将项目(通知弹出窗口)添加到 onBlur 事件。 leave 时动画运行流畅,appear 时动画突然出现,没有transition。最近在React,不强骂:D
附言
如果我在 alert.js 中添加 ReactCSSTransitionGroup -appear 有效,但 leave - 没有。
代码沙箱: https://codesandbox.io/s/l26j10613q
(在 CodeSandbox 上,我在 alert.js 中使用了 ReactCSSTransitionGroup,所以 appear 有效,但是 leave——没有)
alert.js:
export default class Alert extends Component {
render() {
const { icon, text } = this.props;
let classNames = "cards-wrapper-alert";
return (
<div className={classNames}>
<Card zIndex="2">
<Icon icon={icon} eClass="alert-message-icon"/>
<Label text={text} fw="fw-medium" fs="fs-14" fc="c-dark"/>
</Card>
</div>
);
}
}
alert.css:
.alert-appear {
max-height: 0;
overflow: hidden;
}
.alert-appear.alert-appear-active {
max-height: 80px;
transition: max-height 300ms ease-in-out;
}
.alert-leave {
max-height: 80px;
}
.alert-leave.alert-leave-active {
max-height: 0;
overflow: hidden;
transition: max-height 300ms ease-in-out;
}
我在 input.js 中做了什么:
<ReactCSSTransitionGroup
component={this.prepareComponentForAnimation}
transitionName="alert"
transitionEnter={false}
transitionAppear={true}
transitionAppearTimeout={400}
transitionLeaveTimeout={400}>
{this.state.alert ?
<Alert icon={this.state.icon} text={this.state.text}/>
: null}
</ReactCSSTransitionGroup>
示例:
最佳答案
我不得不change your code通过将 ReactCSSTransitionGroup 移动到 Input 组件来重现您在 .gif 中显示的确切场景。
看完docs , 我发现这一行对于为什么你的 Alert 在第一次出现时没有动画是有道理的:
ReactCSSTransitionGroup provides the optional prop transitionAppear, to add an extra transition phase at the initial mount of the component.
这里发生的不是初始挂载。一旦输入有一些用户交互,就会设置 alert 状态。
因此,您的问题的简单答案是您需要使用 enter 阶段,而不是 appear 阶段,因为我在上面发布了文档。
关于javascript - ReactCSSTransitionGroup:transitionAppear 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53259591/