我想做什么

我有一个精灵表,正在将其用于一些简单的动画。我有一个具有两个动画的“播放器”。一种用于进攻,一种用于站立。当前行为如下:“stand”是默认动画。单击播放器后,将播放“攻击”动画并将其切换回“站立”状态。

对于在攻击时单击播放器的特殊情况,我希望重置动画“攻击”动画。

我的密码

// React Component
class Player extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      timeout: null
    };
  }

  attack(e) {
    if (this.state.timeout) { clearTimeout(this.state.timeout); }
    var $el = $(e.target);
    $el.css({
      backgroundPosition: '0 220px',
      animationName: 'attack',
      width: '300px'
    });
    this.state.timeout = setTimeout(function() {
      $el.css({
        backgroundPosition: 'left top',
        animationName: 'stand',
        width: '250px'
      });
    }, 2000);
  }

  render () {
    return (
      <div onClick={this.attack.bind(this)} className="player main">{this.props.name}</div>
    );
  }
}


     /* The css */
     .player {
        width: 250px;
        height: 220px;
        background: url('assets/player.png') left top;
        animation: stand 1.2s steps(3) infinite;
        animation-direction: alternate;
      }
      @keyframes stand {
        100% {
          background-position: -750px 0;
        }
      }
      @keyframes attack {
        100% {
          background-position: -900px 220px;
        }
      }

我试图做的

我知道,由于我将setTimeout设置为“站立”动画,因此必须取消它,并制作一个新的setTimeout才能跟随“stand”动画。但是,它看起来还是有点时髦。我尝试使用forceUpdate()来查看是否可以重新渲染并重置所有内容,然后启动“攻击”动画,但是它似乎没有任何作用。

我有另一个想法,使用jquery删除并重新附加dom元素,但我觉得它会变得凌乱,我相信我可能会以错误的方式解决此问题。你们有什么主意吗?

最佳答案

对于复杂的动画,可以使用https://github.com/chenglou/react-motion,可以从https://egghead.io/playlists/react-react-animation-using-react-motionhttps://egghead.io/lessons/react-react-motion-introduction-to-the-spring-component中学习

对于简单的动画,我宁愿从element中添加和删除类以启用动画并重置它。

如果我正确理解它,则说明您在jQuery内部使用react。好了,要更新CSS,您不应该在react内部使用它。

在React中使用jQuery应该有更强的理由,因为在React中更新内联CSS非常简单。

以下是我尝试执行的方法:

// React Component
class Player extends React.Component {
    constructor(props) {
      super(props);
      this.state = {
        direction: null,
        timeout: null
      };
    }

    attack(e) {

      //following line can be used to identify multiple targets
      //let target = (e.target);
      if (this.state.timeout !== null) {
        window.clearTimeout(this.state.timeout)
      }
      let timeout = setTimeout(() => {
        this.setState({
          direction: null,
          timeout: null
        });
      }, 8000)
      let direction = null;
      if (e.ctrlKey) {
        direction = 'anticlockwise'
      } else {
        direction = 'clockwise'
      }
      if (this.state.direction !== direction) {
        this.setState({
          direction, timeout
        });
      }

    }

    render() {
        let classNames = ["player", "main"];
        let styles = {}
        if (this.state.direction !== null) {
          if (this.state.direction === 'clockwise')
            styles.zoom = 0.8
          else
            styles.zoom = 1.2
          classNames.push(this.state.direction)
        }
        return ( < div onClick = {
            this.attack.bind(this)
          }
          style={styles}
          className = {
            classNames.join(' ')
          } > {
            this.props.name
          } < /div>
    );
  }
}
ReactDOM.render(<Player / > , document.getElementById('game'));
.player {
  width: 100px;
  height: 100px;
  margin: 50px;
  zoom: 1;
  transition: all linear 0.3s;
  display: inline-block;
  background-color: #ccc;
  animation-timing-function: linear;
}
.clockwise {
  animation: clockwise 5s infinite;
}
.anticlockwise {
  animation: anticlockwise 5s infinite;
}
@keyframes clockwise {
  0% {
    transform: rotate(0deg)
  }
  50% {
    transform: rotate(180deg)
  }
  100% {
    transform: rotate(360deg)
  }
}
@keyframes anticlockwise {
  0% {
    transform: rotate(360deg)
  }
  50% {
    transform: rotate(180deg)
  }
  100% {
    transform: rotate(0deg)
  }
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script>
<div id="game"></div>

10-06 12:21
查看更多