【问题标题】:Is it possible to recreate marquee using CSS only?是否可以仅使用 CSS 重新创建选取框?
【发布时间】:2019-11-22 20:10:29
【问题描述】:

我有一个可以使用选取框解决的需求

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

由于 marquee 已被弃用,我们如何使其与 html5 兼容?

我见过一些例子,但大多数都依赖于固定宽度。 在我的示例中,这些项目是从服务器接收的,因此可能会有很多。此外,我需要在悬停时停止,因为这些项目是指向其他内容的链接。

非常感谢您的帮助,

PS:我想确保在我开始探索 javascript 之前,我们不能仅在 CSS 中执行此操作。

【问题讨论】:

  • 从未尝试添加类似的东西,但也许这会有所帮助 -> thoughtco.com/…
  • 是的,有可能。搜索“CSS3 marquee”,你会得到很多结果。
  • Morpheus,我尝试将这些样式添加到
    中,但它不起作用。我在 IE11 和 Chrome 中测试过。

标签: css html css-animations marquee


【解决方案1】:

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

【讨论】:

  • 如果我们在该示例中添加更多 元素,它就不会像选取框那样工作。
  • @herme0 您可以通过将white-space: nowrap 添加到.marquee div {} 并删除.marquee span 的样式来对其进行排序
  • 我在 codepen 示例中这样做了,然后将内部 html 替换为 Another span 1Another span 1...Another span 15 一旦到达跨度#10,它就会重置滚动。我认为这是因为 .marquee div {} 上的宽度:300%。
  • @herme0 对,现在我和你在一起。这是因为动画在5秒后从初始点left: 0重复
【解决方案2】:

抱歉,我知道我迟到了。不过,我有一个简单的解决方案,可以使用 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>

【讨论】:

    【解决方案3】:

    我终于找到了一个可行的,这是最终的产品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 属性上添加动画。

    谢谢大家,

    【讨论】:

      【解决方案4】:

      我不擅长java脚本

      但这里是使用 html 和 css

      附言。鼠标悬停在这里不起作用

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

      【讨论】:

      • 我的示例在包装器中有多个项目。在您提供的示例中添加更多项目会将文本堆叠在一起。
      【解决方案5】:

      google 上有很多关于它的信息,只需搜索:“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>

      【讨论】:

      • 一旦我们在选框元素中有多个 ,帖子中接受的解决方案就不起作用了。
      猜你喜欢
      • 2016-01-27
      • 1970-01-01
      • 2017-03-01
      • 2013-10-07
      • 2014-11-16
      • 1970-01-01
      • 1970-01-01
      • 2020-10-12
      • 1970-01-01
      相关资源
      最近更新 更多