How can I animate a react.js component onclick and detect the end of the animation

后端 未结 6 1494
孤街浪徒
孤街浪徒 2020-12-28 13:19

I want to have a react component flip over when a user clicks on the DOM element. I see some documentation about their animation mixin but it looks to be set up for \"enter\

相关标签:
6条回答
  • 2020-12-28 13:37

    Upon clicks you can update the state, add a class and record the animationend event.

    class ClickMe extends React.Component {
      constructor(props) {
        super(props)
        this.state = { fade: false }
      }
      render() {
        const fade = this.state.fade
    
        return (
          <button
            ref='button'
            onClick={() => this.setState({ fade: true })}
            onAnimationEnd={() => this.setState({ fade: false })}
            className={fade ? 'fade' : ''}>
            Click me!
          </button>
        )
      }
    }
    

    See the plnkr: https://next.plnkr.co/edit/gbt0W4SQhnZILlmQ?open=Hello.js&deferRun=1&preview

    Edit: Updated to reflect current React, which supports animationend events.

    0 讨论(0)
  • 2020-12-28 13:39

    Most popular and easy to use package I came across:

    https://www.npmjs.com/package/react-transition-group

    Install:

    npm install react-transition-group
    

    Usage:

    import { CSSTransition } from 'react-transition-group';
    
    <CSSTransition
      in={toShow} // boolean value passed via state/props to either mount or unmount this component
      timeout={300}
      classNames='my-element' // IMP!
      unmountOnExit
    >
      <ComponentToBeAnimated />
    </CSSTransition>
    

    NOTE: Make sure to apply below styles using the class property in CSS:

    .my-element-enter {
      opacity: 0;
      transform: scale(0.9);
    }
    .my-element-enter-active {
      opacity: 1;
      transform: translateX(0);
      transition: opacity 300ms, transform 300ms;
    }
    .my-element-exit {
      opacity: 1;
    }
    .my-element-exit-active {
      opacity: 0;
      transform: scale(0.9);
      transition: opacity 300ms, transform 300ms;
    }
    
    0 讨论(0)
  • 2020-12-28 13:48

    React uses synthetic events, which includes animation events. Documention found here: https://reactjs.org/docs/events.html#animation-events. I updated the accepted answer below:

    class ClickMe extends React.Component {
      state = {fade: false};
    
      render () {
        const {fade} = this.state;
    
        return (
          <button
            onClick={() => this.setState({fade: true})}
            onAnimationEnd={() => this.setState({fade: false})}
            className={fade ? 'fade' : ''}>
              Click me!
          </button>
        )
      }
    }
    
    0 讨论(0)
  • 2020-12-28 13:48

    Here is the answer using pure Reactjs events without any JQueries, other libs, registrations or sms :)

    The key point is to provide animation keyframe name as a function parameter

    CSS

    .Modal {
        position: fixed;
        top: 30%;
        left: 25%;
        transition: all 0.3s ease-out;
    }
    .ModalOpen {
        animation: openModal 0.4s ease-out forwards;
    }
    .ModalClosed {
        animation: closeModal 0.4s ease-out forwards;
    }
    
    @keyframes openModal {
        0% { transform: translateY(-100%); }
        100% { transform: translateY(0);   }
    }
    
    @keyframes closeModal {
        0% { transform: translateY(0); }
        100% { transform: translateY(-100%);}
    }
    

    JS

    const modal = ({ 
      isShown, isMounted, 
      initiateUnmountAction, unmountAction
    }) => {
      const cssClasses = [
        "Modal",
        props.isShown ? "ModalOpen" : "ModalClosed"
      ];
      return (
        <Fragment>
          {isMounted && <div className={cssClasses.join(' ')}
            onAnimationEnd={event => 
              {event.animationName == "closeModal" && unmountAction}
          }>
            <h1>A Modal</h1>
            <button className="Button" onClick={initiateUnmountAction}>
              Dismiss
          </button>
          </div>}
        </Fragment>
    
      );
    };
    
    0 讨论(0)
  • 2020-12-28 13:55

    I've never used React, but if it uses CSS3 animations/transitions, you might be able to do something like this:

     element.addEventListener( 'webkitTransitionEnd', function( event ) { 
    
       console.log( 'Complete');
    
     }, false );
    
    0 讨论(0)
  • 2020-12-28 13:59

    I successfully used this project in my react-hammer integration project there are some examples with hammer events and react animation.

    0 讨论(0)
提交回复
热议问题