React.js: Non-CSS animations

前端 未结 4 707
暗喜
暗喜 2021-02-07 04:20

React documentation doesn’t have anything about handling animations that are not CSS transitions, such as animation of scroll position and SVG attributes.

As for CSS tra

4条回答
  •  爱一瞬间的悲伤
    2021-02-07 04:30

    I successfully used this project in my react-hammer integration project there are some examples with hammer events and react animation.

    Here code of animated "BlinkingThing":

    var BlinkingThing = React.createClass({
        mixins: [React.Animate],
        blink: function () {
            var that = this;
            var animateAfter = function () {
                that.animate({
                    color: 'green'
                }, that.props.blinkBack);
            };
            this.animate({
                color: 'yellow'
            }, this.props.blinkTo, animateAfter);
        },
        componentDidReceiveProps: function () {
            this.setState({color: this.props.color})
        },
        componentDidMount: function () {
            this.setState({color: this.props.color})
        },
        receiveHammerEvent: function (ev) {
            if (ev) {
                var value = ev.type;
    
                switch (value) {
                    case 'tap':
                        this.blink();
                        break;
                }
            }
        },
        getInitialState: function () {
            return {};
        },
        render: function () {
            var style = {
                display: 'inline-block',
                backgroundColor: this.state.color
            };
    
            return (
    {this.props.children}
    ); } });

    You need some parent component which will propagate side effects(touch events, for example) to trigger changes of state in BlinkingThing component (animation rely on state changing when you call this.animate func), I made a HitArea component to do that. It calls receiveHammerEvent func from its children passing hammer event when it occurs.

提交回复
热议问题