如何创建选取框效果?的答案展示了如何创建选取框效果,但是它在选取框每次迭代的末尾留下了很多空白空间。
有没有办法使用CSS实现平滑的
<marquee></marquee>
效果,并且没有这个空白区域?
我有很多小元素,看起来有点像 Stack Overflow 的蓝色标签,它们专门填充选取框的内容,而不是一个连续的正文或一堵文本墙。
这是一个示例,您可以通过设置延迟和持续时间来控制文本之间的间距
.marquee {
background-color: #ddd;
width: 500px;
margin: 0 auto;
overflow: hidden;
white-space: nowrap;
}
.marquee span {
display: inline-block;
font-size: 20px;
position: relative;
left: 100%;
animation: marquee 8s linear infinite;
}
.marquee:hover span {
animation-play-state: paused;
}
.marquee span:nth-child(1) {
animation-delay: 0s;
}
.marquee span:nth-child(2) {
animation-delay: 0.8s;
}
.marquee span:nth-child(3) {
animation-delay: 1.6s;
}
.marquee span:nth-child(4) {
animation-delay: 2.4s;
}
.marquee span:nth-child(5) {
animation-delay: 3.2s;
}
@keyframes marquee {
0% { left: 100%; }
100% { left: -100%; }
}
<p class="marquee">
<span>this is a</span>
<span>simple marquee</span>
<span>using css</span>
<span>only tech</span>
<span>with a delay</span>
</p>
如果选取框足够大,您可以在动画中间交换其中一个集合。
我认为这是仅使用 CSS 所能达到的极限
.marquee {
width: 100%;
height: 80px;
margin: 0 auto;
overflow: hidden;
white-space: nowrap;
border: 1px solid blue;
}
.marquee-content {
display: inline-block;
margin-top: 5px;
animation: marquee 15s linear infinite;
}
.item-collection-1 {
position: relative;
left: 0%;
animation: swap 15s linear infinite;
}
@keyframes swap {
0%, 50% {
left: 0%;
}
50.01%,
100% {
left: 100%;
}
}
.marquee-content:hover {
animation-play-state: paused
}
.item1 {
display: inline-block;
height: 70px;
width: 140px;
background: cyan;
vertical-align: top;
margin-left: 15px;
}
.item2 {
display: inline-block;
height: 70px;
width: 100px;
background: magenta;
vertical-align: top;
margin-left: 15px;
line-height: 14px;
}
/* Transition */
@keyframes marquee {
0% {
transform: translateX(0)
}
100% {
transform: translateX(-100%)
}
}
<div class="marquee">
<div class="marquee-content">
<span class="item-collection-1">
<span><img src="https://cdn.sstatic.net/Sites/stackoverflow/img/[email protected]" height="80"></span>
<span class="item1"></span>
<span><img src="https://cdn.sstatic.net/Sites/stackoverflow/img/[email protected]" height="80"></span>
<span class="item1"></span>
<span class="item1"></span>
<span class="item1"></span>
<span class="item1"></span>
</span>
<span class="item-collection-2">
<span class="item2"></span>
<span class="item2"></span>
<span class="item2"></span>
<span class="item2"></span>
<span class="item2"></span>
<span class="item2"></span>
<span class="item2"></span>
<span class="item2"></span>
</span>
</div>
</div>
在您提到的示例中,由于内容从 x=0 到 x=-100%,当动画再次开始时,它总是会在末尾留下间隙。
此解决方案的第一步依赖于复制内容。但是,如果我们从 x=0 到 x=-100,尽管内容显示的时间有点长,因为它显示了相同的内容两次,但我们仍然遇到相同的问题。当它完成两轮主要内容的显示后,又出现了一个间隙(这次更大)。
所以我们从 x=0 到 x=-50 求解。当动画到达整个内容的一半时,动画结束,但没有丢失任何内容,因为整个内容的后半部分与主要内容相同。
当动画完成时(即 x=-50%),它将再次开始。但由于填充的一半与即将开始的一半相同,因此看起来像是一个接一个地进行。
希望代码能更好地解释这一点:
.marquee {
--duration: 20;
background-color: #ddd;
width: max-content;
white-space: nowrap;
display: flex;
gap: 1rem;
padding-left: 1rem;
animation: marquee calc(var(--duration) * 1s) linear infinite;
}
@keyframes marquee {
to {
transform: translateX(-50%);
}
}
.main{
background-color: yellow;
}
.duplicated{
background-color: orange;
}
<b>Solution</b>
<p class="marquee">
<span>this is a simple marquee</span>
<span>with a very long text</span>
<span>that I will animate using css</span>
<span>I am making this long enough</span>
<span>so so long long</span>
<span aria-hidden="true">this is a simple marquee</span>
<span aria-hidden="true">with a very long text</span>
<span aria-hidden="true">that I will animate using css</span>
<span aria-hidden="true">I am making this long enough</span>
<span aria-hidden="true">so so long long</span>
</p>
<b>Visual aid</b>
<br/>
<span class="main">main content</span>
<span class="duplicated">duplicated content</span>
<p class="marquee visual-aid">
<span class="main">this is a simple marquee</span>
<span class="main">with a very long text</span>
<span class="main">that I will animate using css</span>
<span class="main">I am making this long enough</span>
<span class="main">so so long long</span>
<span class="duplicated" aria-hidden="true">this is a simple marquee</span>
<span class="duplicated" aria-hidden="true">with a very long text</span>
<span class="duplicated" aria-hidden="true">that I will animate using css</span>
<span class="duplicated" aria-hidden="true">I am making this long enough</span>
<span class="duplicated" aria-hidden="true">so so long long</span>
</p>