能否仅使用CSS重新创建跑马灯效果?

5

我有一个需求,可以使用滚动字幕来解决

.ticker {
  white-space: no-wrap;
}
.item {
  display: inline-block;
  cursor: pointer;
}
<marquee class="ticker" onmouseover="this.stop()" onmouseout="this.start()">
    <div class="item">Item 1</div>
    <div class="item">Item 2</div>
    <div class="item">Item 3</div>
    <div class="item">Item 4</div>
 </marquee>

如何使这符合html5标准,因为marquee已经被弃用了?

我看到了一些例子,但大多数都依赖于固定宽度。在我的例子中,项目是从服务器接收的,因此可能会有很多。而且,由于项目是指向其他内容的链接,因此我需要悬停时停止滚动。

非常感谢您的帮助,

附言:在我开始探索javascript之前,我想确保我们不能仅使用CSS来实现这一点。


1
从未尝试过添加类似的东西,但也许这会有所帮助 -> https://www.thoughtco.com/scrollable-content-html5-css3-without-marquee-3467007 - Morpheus
是的,这是可能的。搜索“CSS3跑马灯”,你会得到很多结果。 - Neil K
莫菲斯,我试图将这些样式添加到一个<div/>中,但它不起作用。我在IE11和Chrome中进行了测试。 - herme 0
5个回答

10

这个codepen有一个很好的例子,可以满足您的需求。

为了让它在悬停时暂停动画,我添加了一个悬停状态来暂停动画: https://codepen.io/anon/pen/zzpZyg

   .marquee:hover div {
      -webkit-animation-play-state: paused; /* Safari 4.0 - 8.0 */
        animation-play-state: paused;
    }

body { margin: 20px; }

.marquee {
  height: 25px;
  width: 420px;

  overflow: hidden;
  position: relative;
}

.marquee div {
  display: block;
  width: 200%;
  height: 30px;

  position: absolute;
  overflow: hidden;

  animation: marquee 5s linear infinite;
}

.marquee span {
  float: left;
  width: 50%;
}

@keyframes marquee {
  0% { left: 0; }
  100% { left: -100%; }
}

.marquee:hover div {
  -webkit-animation-play-state: paused; /* Safari 4.0 - 8.0 */
    animation-play-state: paused;
}
<div class="marquee">
  <div>
    <span>You spin me right round, baby. Like a record, baby.</span>
    <span>You spin me right round, baby. Like a record, baby.</span>
  </div>
</div>


如果我们在那个示例中添加更多的<span/>元素,它就不像跑马灯一样工作。 - herme 0
@herme0,您可以通过将white-space: nowrap添加到.marquee div {}并删除.marquee span的样式来进行排序。 - Morpheus
我在CodePen示例中完成了这个操作,然后用 <span>另一个span 1</span><span>另一个span 2</span>...<span>另一个span 15</span>替换了内部HTML。一旦它到达span#10,它会重置滚动。我认为这是因为.marquee div {}上的width:300%。 - herme 0
@herme0 对,现在我明白了。这是因为动画在5秒后从初始点 left: 0 开始重复播放。 - Morpheus

3

抱歉我来晚了。不过,我有一个使用CSS创建跑马灯的简单解决方案。

.marquee-container{
  overflow:hidden;
}
.marquee{
  min-width:100%;
  animation: marquee 15s linear infinite;
}
.marquee:hover{
  animation-play-state: paused;
}
@keyframes marquee {
  from{margin-left : 120%;}
    to{margin-left: -20%;}
} 
<div class="marquee-container">
    <p class="marquee">
      <span>Item 1</span>
      <span>Item 2</span>
      <span>Item 3</span>
      <span>Item 4</span>
    </p>
</div>


2

我不擅长JavaScript

但是这里使用的是HTML和CSS

PS. 这个鼠标悬停效果在这里无法使用

.wrapper {
  position: relative;
  overflow: hidden;
  height: 25px;
  width: 100px;
  border: 1px solid orange;
}

.wrapper p {
  position: absolute;
  margin: 0;
  line-height: 25px;
  white-space: nowrap;
  animation: marquee 5s linear infinite;
}

@keyframes marquee {
  0% { transform: translateX(100%); }
  100% { transform: translateX(-100%); }
}
<div class="wrapper">
  <p>Hey, how you're doing? .</p>
</div>


我的示例包含多个项目。在您提供的示例中添加更多项目将使文本堆叠在一起。 - herme 0

2
最终我找到了一个可行的方案,以下是最终产品 https://fiddle.sencha.com/#view/editor&fiddle/228u 这里是原始版本 https://codepen.io/lewismcarey/pen/GJZVoG
<div class="wrapper">
    <div class="container">
       <span>Span Me 1</span>
       <span>Span Me 2</span>
       <span>Span Me 3</span>
       <span>Span Me 4</span>
    </div>
</div>

技巧在于“左填充”包装器以最初隐藏容器。然后,“右填充”容器,使得动画仅在容器离开屏幕后停止/重新开始。两个填充都是相对大小的。将display: block;添加到容器中,以便右填充使用包装器的大小。最后,在包装器的transform属性上添加动画。
谢谢大家。

1

谷歌上有很多关于它的信息,只需搜索:“css3 marque”,如Neil Kennedy所述。一个问题与您的非常相似,当您悬停在文本上时,文本停止滚动。请查看下面的链接:CSS3 Marquee Effect和jsfiddle:http://jsfiddle.net/MaY5A/1/

$(".toggle").on("click", function() {
  $(".marquee").toggleClass("microsoft");
});
/* Make it a marquee */

.marquee {
  width: 450px;
  margin: 0 auto;
  white-space: nowrap;
  overflow: hidden;
  box-sizing: border-box;
  border: 1px green solid;
}

.marquee span {
  display: inline-block;
  padding-left: 100%;
  text-indent: 0;
  border: 1px red solid;
  animation: marquee 15s linear infinite;
}

.marquee span:hover {
  animation-play-state: paused
}


/* Make it move */

@keyframes marquee {
  0% {
    transform: translate(0, 0);
  }
  100% {
    transform: translate(-100%, 0);
  }
}


/* Make it pretty */

.microsoft {
  padding-left: 1.5em;
  position: relative;
  font: 16px 'Segoe UI', Tahoma, Helvetica, Sans-Serif;
}


/* ::before was :before before ::before was ::before - kthx */

.microsoft:before,
.microsoft::before {
  z-index: 2;
  content: '';
  position: absolute;
  top: -1em;
  left: -1em;
  width: .5em;
  height: .5em;
  box-shadow: 1.0em 1.25em 0 #F65314, 1.6em 1.25em 0 #7CBB00, 1.0em 1.85em 0 #00A1F1, 1.6em 1.85em 0 #FFBB00;
}

.microsoft:after,
.microsoft::after {
  z-index: 1;
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  width: 2em;
  height: 2em;
  background-image: linear-gradient(90deg, white 70%, rgba(255, 255, 255, 0));
}


/* Style the links */

.vanity {
  color: #333;
  text-align: center;
  font: .75em 'Segoe UI', Tahoma, Helvetica, Sans-Serif;
}

.vanity a,
.microsoft a {
  color: #1570A6;
  transition: color .5s;
  text-decoration: none;
}

.vanity a:hover,
.microsoft a:hover {
  color: #F65314;
}


/* Style toggle button */

.toggle {
  display: block;
  margin: 2em auto;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<!-- Wanted to see how easily marquees could be constructed with CSS:
     - This demo uses -prefix-free to avoid vendor prefixes
     - It also requires manual setting of the end text-indent
     - Everything below the /* Make it pretty */ comment is non-essential
     - Brought to you by @jonathansampson -->
<p class="microsoft marquee"><span>Windows 8 and Windows RT are focused on your life—your friends and family, your apps, and your stuff. With new things like the <a href="http://windows.microsoft.com/en-US/windows-8/start-screen">Start screen</a>, <a href="http://windows.microsoft.com/en-US/windows-8/charms">charms</a>, and a <a href="http://windows.microsoft.com/en-US/windows-8/microsoft-account">Microsoft account</a>, you can spend less time searching and more time doing.</span></p>
<button class="toggle">Toggle Beautification</button>
<p class="vanity">
  <a href="https://twitter.com/jonathansampson">@jonathansampson</a> of
  <a href="https://twitter.com/appendTo">@appendTo</a>
</p>


帖子中被接受的解决方案在 marquee 元素内有多个 <span/> 时不起作用。 - herme 0

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