【发布时间】:2020-05-10 22:57:49
【问题描述】:
我正在开发一个 React AMP 项目,我需要使用 AMP 制作一些微调动画,以便在窗口滚动时显示和隐藏按钮。
由于 AMP Animation 标记需要 <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 中也不起作用。
如果有人有替代方案或我的代码有问题,请提出建议。
【问题讨论】:
-
你试过this SO post中的解决方案吗?
-
这个有答案吗,我也有同样的问题
标签: javascript reactjs amp-html