使用关键帧动画的文本滑块

时间:2019-09-23 08:54:19

标签: html css css-animations

我有一个包含3个项目的滑块,其中的所有项目都可以按我的要求很好地工作

这是现场演示working demo

带有3个项目的文本滑块

HTML

    <span class="item-1">FAST.</span>
    <span class="item-2">SIMPLE.</span>
    <span class="item-3">PERSONAL.</span>

Css

body {
  font-family: 'Open Sans', 'sans-serif';
  color: #cecece;
  background: #222;
  overflow: hidden;
}

.item-1,
.item-2,
.item-3 {
  position: absolute;
  display: block;
  top: 2em;
  width: 60%;
  font-size: 2em;
  animation-duration: 20s;
  animation-timing-function: ease-in-out;
  animation-iteration-count: infinite;
}

.item-1 {
  animation-name: anim-1;
}

.item-2 {
  animation-name: anim-2;
}

.item-3 {
  animation-name: anim-3;
}

@keyframes anim-1 {
  0%,
  8.3% {
    top: -100%;
    opacity: 0;
  }
  8.3%,
  25% {
    top: 25%;
    opacity: 1;
  }
  33.33%,
  100% {
    top: 110%;
    opacity: 0;
  }
}

@keyframes anim-2 {
  0%,
  33.33% {
    top: -100%;
    opacity: 0;
  }
  41.63%,
 58.29%  {
    top: 25%;
    opacity: 1;
  }
  66.66%,
  100% {
    top: 110%;
    opacity: 0;
  }
}

@keyframes anim-3 {
  0%,
  66.66% {
    top: -100%;
    opacity: 0;
  }
  74.96%,
  91.62% {
    top: 25%;
    opacity: 1;
  }
  100% {
    top: 110%;
    opacity: 0;
  }
}

现在我要在滑块上再添加两个项目

HTML

        <span class="item-1">FAST.</span>
        <span class="item-2">SIMPLE.</span>
        <span class="item-3">PERSONAL.</span>
        <span class="item-4">SOCIAL.</span>             
        <span class="item-5">LOUD.</span>  

Css

body {
  font-family: 'Open Sans', 'sans-serif';
  color: #cecece;
  background: #222;
  overflow: hidden;
}

.item-1,
.item-2,
.item-3,
.item-4,
.item-5{
  position: absolute;
  display: block;
  top: 2em;
  width: 60%;
  font-size: 2em;
  animation-duration: 20s;
  animation-timing-function: ease-in-out;
  animation-iteration-count: infinite;
}

.item-1 {
  animation-name: anim-1;
}

.item-2 {
  animation-name: anim-2;
}

.item-3 {
  animation-name: anim-3;
}
.item-4{
    animation-name: anim-4;
}
.item-5{
    animation-name: anim-5;
}

@keyframes anim-1 {
  0%,
  6.3% {
    top: -100%;
    opacity: 0;
  }
  6.3%,
  25% {
    top: 25%;
    opacity: 1;
  }
  13.33%,
  100% {
    top: 110%;
    opacity: 0;
  }
}

@keyframes anim-2 {
  0%,
  23.33% {
    top: -100%;
    opacity: 0;
  }

  31.63%,
  48.29% {
    top: 25%;
    opacity: 1;
  }

  56.66%,
  100% {
    top: 110%;
    opacity: 0;
  }

}

@keyframes anim-3 {
  0%,
  56.66% {
    top: -100%;
    opacity: 0;
  }
  64.96%,
  71.62% {
    top: 25%;
    opacity: 1;
  }
  100% {
    top: 110%;
    opacity: 0;
  }
}

@keyframes anim-4 {
  0%,
  71.66% {
    top: -100%;
    opacity: 0;
  }
  84.96%,
  91.62% {
    top: 25%;
    opacity: 1;
  }
  100% {
    top: 110%;
    opacity: 0;
  }
}

@keyframes anim-5 {
  0%,
  84.96% {
    top: -100%;
    opacity: 0;
  }

  94.96%,
  91.62% {
    top: 25%;
    opacity: 1;
  }

  100% {
    top: 110%;
    opacity: 0;
  }
}

这里是包含五个项目的演示

not working demo

我需要更改我的代码吗?

2 个答案:

答案 0 :(得分:3)

问题是由于五个不同动画的百分比混乱所致。

为什么不重复使用相同的动画,如:

@keyframes anim {
  0%, 33.33% {
    top: -100%;
    opacity: 0;
  }
  33.33%, 50% {
    top: 25%;
    opacity: 1;
  }
  50%, 100% {
    top: 110%;
    opacity: 0;
  }
}

,然后在每个animation-delay上应用span,例如:

.item-1 { animation-delay: 0s }
.item-2 { animation-delay: 4s }
.item-3 { animation-delay: 8s }
.item-4{ animation-delay: 12s }
.item-5{ animation-delay: 16s }

Here是一个有效的示例。

提示,请记住,对top值进行动画处理并不是性能方面的最佳选择。尽可能尝试为transformopacity值设置动画。

答案 1 :(得分:0)

body {
  font-family: 'Open Sans', 'sans-serif';
  color: #cecece;
  background: #222;
  overflow: hidden;
}

.item-1,
.item-2,
.item-3,
.item-4,
.item-5{
  position: absolute;
  display: block;
  top: -100%;
  width: 60%;
  opacity: 0;
  font-size: 2em;
  animation-duration: 15s;
  animation-timing-function: ease-in-out;
  animation-iteration-count: infinite;
  animation-name: anim-all;
}

.item-1 { animation-delay: 0s }
.item-2 { animation-delay: 3s }
.item-3 { animation-delay: 6s }
.item-4{ animation-delay: 9s }
.item-5{ animation-delay: 12s }

@keyframes anim-all {
  0%, 33.33% {
    top: -100%;
    opacity: 0;
  }
  33.33%, 50% {
    top: 25%;
    opacity: 1;
  }
  50%, 100% {
        top: 110%;
    opacity: 0;
  }
}
<body>
<span class="item-1">FAST.</span>
<span class="item-2">SIMPLE.</span>
<span class="item-3">PERSONAL.</span>
<span class="item-4">SOCIAL.</span> 			
<span class="item-5">LOUD.</span>
</body>

body {
  font-family: 'Open Sans', 'sans-serif';
  color: #cecece;
  background: #222;
  overflow: hidden;
}

.item-1,
.item-2,
.item-3,
.item-4,
.item-5{
  position: absolute;
  display: block;
  top: -100%;
  width: 60%;
  opacity: 0;
  font-size: 2em;
  animation-duration: 20s;
  animation-timing-function: ease-in-out;
  animation-iteration-count: infinite;
  animation-name: anim-all;
}

.item-1 { animation-delay: 0s }
.item-2 { animation-delay: 4.25s }
.item-3 { animation-delay: 8.50s }
.item-4{ animation-delay: 12.75s }
.item-5{ animation-delay: 17s }

@keyframes anim-all {
  0%, 33.33% {
    top: -100%;
    opacity: 0;
  }
  33.33%, 50% {
        top: 25%;
    opacity: 1;
  }
  50%, 100% {
        top: 110%;
    opacity: 0;
  }
}
<body>
<span class="item-1">FAST.</span>
<span class="item-2">SIMPLE.</span>
<span class="item-3">PERSONAL.</span>
<span class="item-4">SOCIAL.</span> 			
<span class="item-5">LOUD.</span>
</body>