Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to replay a CSS3 animation in Reactjs

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>)
    }    
}
like image 632
Atlante Avila Avatar asked Sep 08 '18 01:09

Atlante Avila


1 Answers

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

like image 175
Shubham Yerawar Avatar answered Oct 22 '22 19:10

Shubham Yerawar