【发布时间】:2020-10-04 10:35:02
【问题描述】:
我希望能够将数组的长度应用于情感样式的组件,这样如果数组中有 2 或 3 个项目,代码就不会改变。
如果项目数组是 2
animation: ${slideNews2} 6s infinite;
如果项目数组是 3
animation: ${slideNews3} 9s infinite;
我想要实现的目标
animation: ${`slideNews+arrayNum`} arrayNum * 3s infinite;
https://codesandbox.io/s/dreamy-knuth-k1vrz?file=/src/App.js
import React from "react";
import styled from "@emotion/styled";
import { keyframes } from "@emotion/core";
const slideNews2 = keyframes`
0% { top: 100% }
5% { top: 0 }
45% { top: 0 }
50% { top: -135% }
100% { top: -135% }
`;
const slideNews3 = keyframes`
0 % { top: 100% }
2 % { top: 0 }
31 % { top: 0 }
33 % { top: -135% }
71 % { top: -135% }
73 % { top: -270 }
100 % { top: -270% }
`;
const InfoTitle = styled.div`
animation: ${slideNews2} 6s infinite;
`;
const App = () => {
const array = [
{
title: "news1",
content: "brabrabrabra1"
},
{
title: "news2",
content: "brabrabrabra2"
},
{
title: "news3",
content: "brabrabrabra3"
}
];
const arrayNum = array.length;
console.log(arrayNum);
return (
<>
{array.map((item, i) => (
<InfoTitle key={i} index={i}>
{item.title}
</InfoTitle>
))}
</>
);
};
export default App;
【问题讨论】: