How to replay a CSS3 animation in Reactjs

偶尔善良 提交于 2021-02-04 17:57:05

问题


I have set my animation in my css file:

 @-webkit-keyframes jello-horizontal{
    0%{
        -webkit-transform:scale3d(1,1,1);
        transform:scale3d(1,1,1)
    }
    30%{
        -webkit-transform:scale3d(1.25,.75,1);
        transform:scale3d(1.25,.75,1)
    }
    40%{
        -webkit-transform:scale3d(.75,1.25,1);
        transform:scale3d(.75,1.25,1)
    }
    50%{
        -webkit-transform:scale3d(1.15,.85,1);
        transform:scale3d(1.15,.85,1)
    }
    65%{
        -webkit-transform:scale3d(.95,1.05,1);
        transform:scale3d(.95,1.05,1)
    }
    75%{
        -webkit-transform:scale3d(1.05,.95,1);
        transform:scale3d(1.05,.95,1)
    }
    100%{
        -webkit-transform:scale3d(1,1,1);
        transform:scale3d(1,1,1)
    }
}
@keyframes jello-horizontal{
    0%{
        -webkit-transform:scale3d(1,1,1);
        transform:scale3d(1,1,1)
    }
    30%{
        -webkit-transform:scale3d(1.25,.75,1);
        transform:scale3d(1.25,.75,1)
    }
    40%{
        -webkit-transform:scale3d(.75,1.25,1);
        transform:scale3d(.75,1.25,1)
    }
    50%{
        -webkit-transform:scale3d(1.15,.85,1);
        transform:scale3d(1.15,.85,1)
    }
    65%{
        -webkit-transform:scale3d(.95,1.05,1);
        transform:scale3d(.95,1.05,1)
    }
    75%{
        -webkit-transform:scale3d(1.05,.95,1);
        transform:scale3d(1.05,.95,1)
    }
    100%{
        -webkit-transform:scale3d(1,1,1);
        transform:scale3d(1,1,1)
    }
}
 .jello-horizontal{
    -webkit-animation:jello-horizontal .9s both;
    animation:jello-horizontal .9s both
}

It works perfectly when mycomponent first loads, but how do I make my component load every time I click a particular button?

My component is pretty complex and can add the code here but I don't think it's necessary as I just need an example of how to trigger the animation on button click. Not sure if this is even possible. The button I click would update a piece of state here's an example of a simplified version of the component:

import React, {Component} from 'react';

export class Cards extends Component {
  constructor() {
    super();
    this.state = {
      flashCardIndex: 0,
      cards: [],
      currentCard: undefined,
      rightActive: true,
      leftActive: true,
      cardError: false,
      nowPlaying: false,
      showLoader: false,
      animateCard: true,
    };

    render() {
       return (<div className="flash-card jello-horizontal">
                  <a href="">Run animation</a>
              </div>)
    }    
}

回答1:


ReactJS uses key attribute to distinguish between elements in the diffing algorithm. So, the first time your component loads , react assumes it as new element and renders it accordingly. But next time it tries to render the same element with different data , react feels that it has the same structure and only replaces the new data with old data.

So to apply animations for every change, we need to tell react that this is a new component. This can be done by providing a key to the element with your animation class. And key can be randomly generated using any random number generator( I generally use shortid).

For working example, please refer: this



来源:https://stackoverflow.com/questions/52231320/how-to-replay-a-css3-animation-in-reactjs

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!