【发布时间】:2026-01-15 01:05:02
【问题描述】:
我正在尝试创建一个无限的水平“滚动”,如选框效果(例如like this one)。
这是我的代码:
.parent {
border: 1px solid black;
width: 100%;
height: 2rem;
}
.container {
height: 100%;
display: flex;
padding-left: 10%;
border: 1px solid tomato;
animation: marquee 5s linear infinite;
}
@keyframes marquee {
0% {
transform: translate(0%, 0);
}
100% {
transform: translate(-100%, 0);
}
}
.child1 {
width: 10rem;
height: 100%;
background-color: #84B7DF;
}
.child2 {
width: 18rem;
height: 100%;
background-color: #f58db6;
}
.child3 {
width: 13rem;
height: 100%;
background-color: #ffc410;
}
.child4 {
width: 21rem;
height: 100%;
background-color: #C8E7C1;
}
<div class="parent">
<div class="container">
<div class="child1"></div>
<div class="child2"></div>
<div class="child3"></div>
<div class="child4"></div>
</div>
</div>
如您所见,它有效,但并不完美。 我希望绿色矩形移动后,立即出现蓝色(略微间隔)的矩形,我不希望有一个完整的白色屏幕。
我希望我的意思很清楚......
非常感谢!
【问题讨论】:
-
您必须手动循环元素。这意味着复制开头的那些并将其附加到后面。你不断地这样做,然后你就有了效果。
-
调整
.child元素的宽度,使其总和等于.container的宽度 -
@Nimsrules 你能举个例子吗?这是一个简单的例子,假设我没有固定宽度的子元素,但它们的宽度取决于它们的内容..
-
@CodeSpirit 你能举个例子吗?
-
查看这篇文章 - *.com/questions/36433961/…
标签: javascript css scroll marquee