是否可以仅使用CSS创建选取框?

问题描述 投票:2回答:4

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

.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已被弃用?

我看过几个例子,但大多数都依赖于固定宽度。在我的示例中,从服务器接收项目,因此可能有很多项目。此外,我将需要停止悬停,因为项目是链接到其他东西。

非常感谢您的帮助,

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

css html5 css-animations marquee
4个回答
5
投票

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>

1
投票

谷歌有很多关于它的信息,只是搜索:“css3 marque”,就像尼尔肯尼迪提到的那样。一个问题与您的问题非常相似,并且在您将鼠标悬停在文本上后文本会停止滚动。请查看以下链接: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>

1
投票

最后我找到了一个有效的,这里是最终产品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
投票

我不擅长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>
© www.soinside.com 2019 - 2024. All rights reserved.