gpt4 book ai didi

javascript - React 组件的上滑过渡

转载 作者:太空宇宙 更新时间:2023-11-03 22:50:42 25 4
gpt4 key购买 nike

我刚刚开始在我的 React.js 元素中使用动画/过渡。在下面的 GIF 中,当我关闭 Message 组件时,它会淡出。在它消失后,我使用 onAnimationEnd 隐藏组件。

我想要发生的是当 Message 消失时让下面的组件向上滑动。我在概念上不确定如何通过 CSS 动画/过渡或通过特定于 React 的方式来完成此操作。

enter image description here

Message.js

import React, {PropTypes, Component} from 'react';
import MessageTypes from '../constants/MessageTypes';

export default class Message extends Component {

constructor(props, context) {
super(props, context);
this.handleClick = this.handleClick.bind(this);
this.onAnimationEnd = this.onAnimationEnd.bind(this);
this.state = {
animate: false,
hide: false
}
}

handleClick(e) {
e.preventDefault();
this.setState({animate: true});
}

onAnimationEnd() {
console.log('fdsfd');
this.setState({hide: true});
}

render() {
return (
<div ref="message" onAnimationEnd={this.onAnimationEnd} className={`card message ${this.state.hide ? 'hide open' : ''} ${this.state.animate ? 'message-transition-fade-out' : ''}`}>
<span className={`icon icon-${this.iconLevel()}`} style={{color: `#${this.iconColor()}`}}></span>
<p>
<strong>{this.props.title}</strong><br />
<span dangerouslySetInnerHTML={{ __html: this.props.message }} />
</p>
<a href="#" onClick={this.handleClick}>
<span className="icon icon-close"></span>
</a>
</div>
);
}

}

Message.scss

.message {

max-height: 150px;
transition: height 2s ease;

&.open {
height: 0; //define your height or use max-height
}

&-transition {

&-fade-out {

@include animation(0s, .3s, fadeout);

}

}
}

最佳答案

我建议您使用 css transitions

将名为 open 的类附加到 Message 组件的根目录。 onAnimationEnd 移除类open。现在,使用 height 为该类设置动画。

伪代码。

.message {
height: 0px;
transition: height 0.3s ease;
}

.message.open {
height: 100px; //define your height or use max-height
}

关于javascript - React 组件的上滑过渡,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40066180/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com