0

I use this inline transform to iterate dynamically over slides

<div className="slides" style={{transform: `translate(${currentPosition}%, 0px)`, left: 0}}> {slider} </div>
</div>

which is working fine. However I would like to add some fadein to this element on position changing and the following css does not working

on element slides via css

opacity: 0;
animation: fadein 2s ease-in 1 forwards;

here is my fadein animation

@keyframes fadein {
  from {
    opacity:0;
  }
  to {
    opacity:1;
  }
}

What I'm missing in this case?

2
  • It works. Fiddle Commented Jun 27, 2017 at 11:32
  • yes this works on component init but if I change position on click than does not work Commented Jun 27, 2017 at 11:44

1 Answer 1

3

You can try something like this. That will give you idea how can you apply animations.

class Test extends React.Component {
  constructor(props) {
    super(props);

    this.state = {
      currentPosition: 0,
      opacity: 0
    }
  }

  componentDidMount() {
    setTimeout(() => this.setState({
      opacity: 1
    }), 500);
  }

  handleClick() {
    let self = this;
    this.setState({
      opacity: 0
    }, () => setTimeout(() => self.setState({
      opacity: 1
    }), 2000))
  }

  render() {
    return ( <
      div >
      <
      div className = "slides"
      style = {
        {
          transform: `translate(${this.state.currentPosition}%, 0px)`,
          left: 0,
          opacity: this.state.opacity
        }
      } > Some title to test animation < /div> <
      button onClick = {
        this.handleClick.bind(this)
      } > Click < /button> <
      /div>
    )
  }
}

React.render( < Test / > , document.getElementById('container'));
.slides {
  transition: all 2s ease-in;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/0.14.8/react.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/0.14.8/react-dom.min.js"></script>
<div id="container"></div>

Here is the fiddle.

Hope it helps.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.