简体   繁体   English

如何在React JS中重新运行CSS3动画

[英]How to rerun css3 animations in react js

I need to show a quick flash message for few seconds after ajax success. 我需要在ajax成功后的几秒钟内显示快速刷新消息。 The message should be auto hidden after 1/2 seconds. 该消息应在1/2秒后自动隐藏。

As reactjs does not promote for dom manipulation, I want to achieve the same using css3 animation. 由于reactjs不促进dom操作,因此我想使用css3动画实现相同的效果。

Here is simple demo i prepared, 这是我准备的简单演示,

When you click on the button for the first time it shows the message, but on the subsequent click it does not do anything. 当您第一次单击该按钮时,它会显示消息,但是在随后的单击中,它不会执行任何操作。

 class Test extends React.Component { state = { show: false } showFlashMessage() { this.setState({ show: true }) } render() { return ( < div > < div className = { `message ${this.state.show ? "fadeOut": ""}` } > Hello < /div> < button id = "but" onClick = { this.showFlashMessage.bind(this) } > Click Me < /button> < /div> ) } } ReactDOM.render( < Test / > , document.getElementById('container') ); 
 @keyframes FadeAnimation { 0% { opacity: 1; visibility: visible; } 100% { opacity: 0; visibility: hidden; } } .fadeOut { animation: FadeAnimation 1s ease-in .4s forwards; } .message { visibility: hidden; } 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script> <div id="container"> <!-- This element's contents will be replaced with your component. --> </div> 

JS Fiddle: http://jsfiddle.net/2kqv6fo7/9/ JS小提琴: http : //jsfiddle.net/2kqv6fo7/9/

I am not sure if there is any other alternatives except css3 . 我不确定css3以外是否还有其他选择。

Any suggestion? 有什么建议吗?

As I have answered to your previous question also, its the same, try handling with the state change. 正如我也回答了您先前的问题一样,请尝试处理状态更改。 Since you have mentioned you are using a toggle button in your previous question, i have used a similar checkbox which will be helpful for you. 既然您已经提到您在上一个问题中使用了toggle button ,那么我使用了类似的checkbox ,这将对您有所帮助。

I ve made the code work only when the toggle is checked , I assume you would need to show some message on toggle off too. 我仅在checked切换时才使代码起作用,我认为您也需要在toggle off显示一些消息。 Do the same, by toggling between two classes. 通过在两个类之间切换来执行相同的操作。 And try to avoid inline function bindings, cuz it will trigger for every render. 并尝试避免内联函数绑定,因为它将为每个渲染触发。 Instead go for constructor, which will get executed just once when the component is loaded. 取而代之的是构造函数,该构造函数在加载组件时仅执行一次。

 class Test extends React.Component { constructor() { super(); this.state = { show: false, showClass: undefined } this.showFlashMessage = (event) => this._showFlashMessage(event); } _showFlashMessage(event) { this.setState({ show: !this.state.show, showClass: (!this.state.show ? "fadeOut": "fadeOut2") }) } render() { return ( < div > < div className = { `message ${this.state.showClass}` } > Hello < /div> <button id ="but" onClick = {this.showFlashMessage} > Click Me </button>< /div> ) } } ReactDOM.render( < Test / > , document.getElementById('container') ); 
 @keyframes FadeAnimation { 0% { opacity: 1; visibility: visible; } 100% { opacity: 0; visibility: hidden; } } @keyframes FadeAnimation2 { 0% { opacity: 1; visibility: visible; } 100% { opacity: 0; visibility: hidden; } } .fadeOut { opacity: 1; visibility: visible; animation: FadeAnimation 1s ease-in .2s forwards; } .fadeOut2 { opacity: 1; visibility: visible; animation: FadeAnimation2 1s ease-in .2s forwards; } .message { opacity: 0; visibility: hidden; } 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script> <div id="container"> <!-- This element's contents will be replaced with your component. --> </div> 

You should capture the end of the animation and change the state there. 您应该捕获动画的结尾并在那里更改状态。

See this: 看到这个:

How can I animate a react.js component onclick and detect the end of the animation 我该如何为react.js组件onclick设置动画并检测动画的结束

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

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