简体   繁体   English

如何为 react.js 组件 onclick 设置动画并检测动画的结束

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

I want to have a react component flip over when a user clicks on the DOM element.当用户单击 DOM 元素时,我希望有一个反应组件翻转。 I see some documentation about their animation mixin but it looks to be set up for "enter" and "leave" events.我看到了一些关于他们的动画混合的文档,但它看起来是为“进入”和“离开”事件设置的。 What is the best way to do this in response to some user input and be notified when the animation starts and completes?响应某些用户输入并在动画开始和完成时收到通知的最佳方法是什么? Currently I have a list item and I want it to flip over an show a few buttons like delete, edit, save.目前我有一个列表项,我希望它翻转显示一些按钮,如删除、编辑、保存。 Perhaps I missed something in the docs.也许我错过了文档中的某些内容。

animation mixin动画混合

http://facebook.github.io/react/docs/animation.html http://facebook.github.io/react/docs/animation.html

Upon clicks you can update the state, add a class and record the animationend event.单击后,您可以更新状态、添加类并记录animationend事件。

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查看plnkr: https ://next.plnkr.co/edit/gbt0W4SQhnZILlmQ ? open = Hello.js & deferRun =1& preview

Edit : Updated to reflect current React, which supports animationend events.编辑:更新以反映当前支持 animationend 事件的 React。

React uses synthetic events, which includes animation events. React 使用合成事件,其中包括动画事件。 Documention found here: https://reactjs.org/docs/events.html#animation-events .文档在这里找到: 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>
    )
  }
}

Here is the answer using pure Reactjs events without any JQueries, other libs, registrations or sms :)这是使用纯 Reactjs 事件的答案,没有任何 JQueries、其他库、注册或短信 :)

The key point is to provide animation keyframe name as a function parameter关键是提供动画关键帧名称作为函数参数

CSS 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 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>

  );
};

I've never used React , but if it uses CSS3 animations/transitions , you might be able to do something like this:我从未使用过React ,但如果它使用CSS3 animations/transitions ,你也许可以做这样的事情:

 element.addEventListener( 'webkitTransitionEnd', function( event ) { 

   console.log( 'Complete');

 }, false );

我在我的 react-hammer 集成项目中成功地使用了这个项目,有一些关于锤子事件和反应动画的例子。

It can also be done using CSS,也可以使用 CSS 来完成,

If you are using TailwindCSS, the answer would be, Add below Classes If你使用 TailwindCSS,答案是,在类下面添加

hover:opacity-90 focus:opacity-100

else raw css else原始 css

.class-name:hover{
    opacity: 0.9;
}
.class-name:focus{
    opacity: 1;
}

How to do it with Hooks and prevState so as to not update state inside your event handlers.如何使用 Hooks 和 prevState 做到这一点,以便不更新事件处理程序中的状态。

 import { useState } from 'react'

export default function Animation() {
  const [fade, setFade] = useState(false)
  
  const triggerFade = () => {
    setFade(prevState => {
      return !prevState
    })
  }
  
  return (
    <div
      onAnimationEnd={triggerFade}
      className={fade ? 'fadedClass' : 'visibleClass'}
    >
      Watch me fade
    </div>
    <button onClick={triggerFade}>Click Me</button>
  )
}

Most popular and easy to use package I came across:我遇到的最受欢迎且易于使用的软件包:

https://www.npmjs.com/package/react-transition-group 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:注意:确保使用 CSS 中的 class 属性应用以下样式:

.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;
}

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

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