我有一个带有一些子元素的div,里面有滚动动画,这是我的代码:
const { useRef, useEffect, useState } = React;
function Card({ cardName, refCard }) {
return (
<div className="bubble" ref={refCard}>
<div className="card m-2 pt-2">
<div className="py-1">
<div className="fs-5 mt-2">{cardName}</div>
</div>
</div>
</div>
);
}
let data = [
{
Name: "1"
},
{
Name: "2"
},
{
Name: "3"
},
{
Name: "4"
},
{
Name: "5"
}
];
function App() {
const ref = useRef(null);
const [containerWidth, setWidth] = useState(100 + "%");
const [animationState, setPlay] = useState("paused");
useEffect(() => {
if (ref.current) {
setWidth(ref.current.scrollWidth + "px");
setPlay("running");
}
}, []);
console.log(containerWidth);
const renderCards = data.map((el, index) => {
return <Card key={index} cardName={el.Name} />;
});
return (
<div className="App">
<div
className="d-flex"
ref={ref}
style={{
width: `${containerWidth}`,
animationPlayState: animationState
}}
>
{renderCards}
{renderCards}
</div>
</div>
);
}
ReactDOM.createRoot(document.getElementById('app')).render(<App/>);
.App {
overflow: hidden;
}
.card {
width: 200px !important;
height: 200px;
background: #ffefef;
box-shadow: 0px 1px 4px 1px rgba(158, 151, 151, 0.25);
border-radius: 15px;
margin: 12px;
padding: 12px;
}
.d-flex {
overflow: visible;
animation: animateContainer 5s linear forwards infinite;
}
@keyframes animateContainer {
from {
transform: translateY(0);
}
to {
transform: translateY(-50%);
}
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/18.2.0/umd/react.production.min.js" integrity="sha512-8Q6Y9XnTbOE+JNvjBQwJ2H8S+UV4uA6hiRykhdtIyDYZ2TprdNmWOUaKdGzOhyr4dCyk287OejbPvwl7lrfqrQ==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/18.2.0/umd/react-dom.production.min.js" integrity="sha512-MOCpqoRoisCTwJ8vQQiciZv0qcpROCidek3GTFS6KTk2+y7munJIlKCVkFCYY+p3ErYFXCjmFjnfTTRSC1OHWQ==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
<div id="app"></div>
在本例中,由于( animation: animateContainer 5s linear forwards infinite;
),我只有5个子元素要显示每个5,但如果我有 animation: animateContainer 5s linear forwards infinite;
个子元素,我不能看到所有项目.我试着把5s换成另一个变量,但是速度变了,而且速度太快了,所以我想要同样的速度,你知道怎么实现吗?如果你有另一个 idea ,我很乐意,我不确定css动画是否能胜任.