如何创建没有空白间隙的CSS跑马灯效果

14

如何创建跑马灯效果?的答案展示了如何创建跑马灯效果,但是在每次迭代结束时会留下很多空白空间。

有没有一种使用CSS实现平滑的<marquee></marquee>效果,而不会有这种空白空间呢?

我有很多小元素,看起来有点像Stack Overflow的蓝色标签,它们专门填充跑马灯的内容,而不是一个连续的主体或一堵墙的文本。


@LGSon,是的-谢谢你的回答。我不记得为什么以前没有接受它,但我可能在等待尝试并忘记了。 - Lucien
3个回答

23

以下是一个示例,您可以通过设置延迟和持续时间来控制文本之间的间距。

.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>


1
如何在“这是一个…” onload时启动? - saleem ahmed
2
@saleemahmed 不确定我是否理解。此动画从页面加载开始。如果您想使用加载事件启动它,只需将 animation: marquee 8s linear infinite; 移动到一个独立的类中,在事件处理程序中将该类添加到 marquee 元素即可。 - Asons
1
谢谢兄弟,最好的想法。 - TeT Psy

4

3
如果跑马灯足够大,您可以在动画中间交换其中一个集合。
我认为这已经是只使用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/apple-touch-icon@2.png" height="80"></span>
        <span class="item1"></span>
        <span><img src="https://cdn.sstatic.net/Sites/stackoverflow/img/apple-touch-icon@2.png" 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>


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接