无法使动画与AMP和React一起使用

时间:2019-03-26 10:39:45

标签: javascript reactjs amp-html

我正在做一个React AMP项目,我需要用AMP做一些微妙的动画,以便在滚动窗口时显示和隐藏一个按钮。

由于AMP动画标签期望<amp-animation>的子级中有一个对象,但是React不允许该对象作为其子级。

这是我尝试使用的代码:

import React from 'react';
const showAnim = {
  "duration": "200ms",
  "fill": "both",
  "iterations": "1",
  "direction": "alternate",
  "animations": [
    {
      "selector": "#download-button",
      "keyframes": [
        { "opacity": "1", "visibility": "visible" }
      ]
    }
  ]
}
const hideAnim = {
  "duration": "200ms",
  "fill": "both",
  "iterations": "1",
  "direction": "alternate",
  "animations": [
    {
      "selector": "#download-button",
      "keyframes": [
        { "opacity": "0", "visibility": "hidden" }
      ]
    }
  ]
};
export default class Animate extends React.Component {

  componentDidMount() {
    window.addEventListener('scroll', this.onScroll)
  }

  onScroll = () => {
    console.log('scrolling')
  }

  renderShowAnimation = () => <amp-animation id="showAnim" layout="nodisplay" src="">
    <script type="application/json">
      {showAnim}
    </script>
  </amp-animation >;

  renderHideAnimation = () => <amp-animation id="showAnim" layout="nodisplay">
    <script type="application/json">
    {hideAnim}
    </script>
  </amp-animation >;


  render() {
    return (
      <main onScroll={this.onScroll} >
        <div>
         {this.renderShowAnimation()}
          {this.renderHideAnimation()}
              <div className="download-button" id="download-button" role="button">
                Download
                <amp-position-observer
                  on="enter:hideAnim.start; exit:showAnim.start"
                  layout="nodisplay">
                </amp-position-observer>
              </div>
        </div>
      </main>
    )
  }
}

现在,当我尝试运行该应用程序时,出现以下错误:

Objects are not valid as a React child (found: object with keys {duration, fill, iterations, direction, animations}).

我也尝试放置一个onScroll事件,但是它在AMP中也不起作用。 如果有人使用辅助词或我的代码有问题,请提出建议。

2 个答案:

答案 0 :(得分:0)

amp-animation希望JSON作为子对象,而不是Javascript对象。 (它们的语法相似,但有所不同。)即使您使用JSON语法编写了对象,但showAnimhideAnim最终还是被javascript解释为对象。您可以使用JSON.stringify()将它们转换为JSON。

如果您看一看他们的examples中的一个,则可以简化您的问题。

<amp-animation layout="nodisplay">
  <script type="application/json">
    {
      "selector": "#target-id",
      "duration": "1s",
      "keyframes": {"opacity": 1}
    }
  </script>
</amp-animation>

问题是,如果将其粘贴到React的render方法中,则会出错,因为{逃脱了jsx并且现在需要javascript。解决此问题的一种方法是使用React的dangerouslySetInnerHTML

<amp-animation layout="nodisplay">
  <script type="application/json" dangerouslySetInnerHTML={{__html: 
    JSON.stringify(
      {
        "selector": "#target-id",
        "duration": "1s",
        "keyframes": {"opacity": 1}
      }
    )
  }}>
  </script>
</amp-animation>

根据您的情况,您可以将两个功能更改为

renderShowAnimation = () => <amp-animation id="showAnim" layout="nodisplay" src="">
    <script type="application/json" dangerouslySetInnerHTML={{__html: JSON.stringify(showAnim)}}>
    </script>
  </amp-animation >;

  renderHideAnimation = () => <amp-animation id="showAnim" layout="nodisplay">
    <script type="application/json" dangerouslySetInnerHTML={{__html: JSON.stringify(showAnim)}}>
    </script>
  </amp-animation >

答案 1 :(得分:-1)

AMP不允许定义自定义javascript(不受限制的js)和自定义事件监听器(on属性除外)。您可以使用amp-bind组件和amp-script组件在AMP中实现一些动态功能