首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >带有React高级组件的Anime.js动画

带有React高级组件的Anime.js动画
EN

Stack Overflow用户
提问于 2019-07-31 12:52:59
回答 1查看 761关注 0票数 1

我有一个功能组件,它呈现网格元素。我想通过将Anime.js动画封装到这个组件中。问题是‘如何以正确的方式实现它,以及如何从WrappedComponent中选择所需的目标元素?’。

代码语言:javascript
复制
import React, { PureComponent } from 'react';
import anime from 'animejs/lib/anime.es.js';

function withAnimation(WrappedComponent) {

    return class extends PureComponent {

        handleAnimation = () => {
            anime({
                targets: 'targets are in WrappedComponent',
                translateY: [-30, 0],
                easing: 'easeInOutQuad',
                duration: 2000,
            })
        }

        componentWillMount(){
            this.handleAnimation()
        }

        render() {
            return <WrappedComponent {...this.props}/>;
        }
    };
}


export default withAnimation;
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-07-31 13:01:30

在父组件中创建一个参考,并将其传递给包装好的组件,并将其用作targets

代码语言:javascript
复制
import React, { PureComponent } from "react";
import anime from "animejs/lib/anime.es.js";

function withAnimation(WrappedComponent) {
  return class extends PureComponent {
    constructor() {
      super();

      // create DOM reference
      this.target1 = React.createRef();
    }

    handleAnimation = () => {
      anime({
        targets: this.target1,
        translateY: [-30, 0],
        easing: "easeInOutQuad",
        duration: 2000
      });
    };

    componentWillMount() {
      this.handleAnimation();
    }

    setTarget = el => {
      this.target1 = el;
    };

    render() {
      return <WrappedComponent setTarget={this.setTarget} {...this.props} />;
    }
  };
}

const WrappedComponent = props => {
  return <div ref={el => props.setTarget(el)}>Animate me</div>;
};

export default withAnimation;
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57291204

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档