仅使用CSS的Rainbow文字动画

Aza*_*zel 2 css html5 colors css3 keyframe

我从这个Rainbow Text Animation Rainbow-Text-animation中得到了启发,我想仅使用CSS来实现这一目标,而不是使用所有复杂的SCSS编码。

到目前为止,我已经得到了我喜欢的东西,现在我只想使其从左到右移动并且在一整行中具有多种颜色,而不是一次仅具有一种颜色。

运行代码片段以查看我在说什么。

如您所见,我希望一行中有尽可能多的颜色,而不是整行中有一种颜色(一次仅一种)。

#shadowBox {
  background-color: rgb(0, 0, 0);
  /* Fallback color */
  background-color: rgba(0, 0, 0, 0.2);
  /* Black w/opacity/see-through */
  border: 3px solid;
}

.rainbow {
  text-align: center;
  text-decoration: underline;
  font-size: 32px;
  font-family: monospace;
  letter-spacing: 5px;
  animation: colorRotate 6s linear 0s infinite;
}

@keyframes colorRotate {
  from {
    color: #6666ff;
  }
  10% {
    color: #0099ff;
  }
  50% {
    color: #00ff00;
  }
  75% {
    color: #ff3399;
  }
  100% {
    color: #6666ff;
  }
}
Run Code Online (Sandbox Code Playgroud)
<div id="shadowBox">

  <h3 class="rainbow">COMING SOON</h3>

</div>
Run Code Online (Sandbox Code Playgroud)

Aus*_*and 7

您可以通过使用渐变背景,从颜色到透明以及从背景剪辑到文本来实现此效果。

#shadowBox {
    background-color: rgb(0, 0, 0);
    /* Fallback color */
    background-color: rgba(0, 0, 0, 0.2);
    /* Black w/opacity/see-through */
    border: 3px solid;
}

.rainbow {
    text-align: center;
    text-decoration: underline;
    font-size: 32px;
    font-family: monospace;
    letter-spacing: 5px;
}
.rainbow_text_animated {
    background: linear-gradient(to right, #6666ff, #0099ff , #00ff00, #ff3399, #6666ff);
    -webkit-background-clip: text;
    background-clip: text;
    color: transparent;
    animation: rainbow_animation 6s ease-in-out infinite;
    background-size: 400% 100%;
}

@keyframes rainbow_animation {
    0%,100% {
        background-position: 0 0;
    }

    50% {
        background-position: 100% 0;
    }
}
Run Code Online (Sandbox Code Playgroud)
<div id="shadowBox">
    <h3 class="rainbow rainbow_text_animated">COMING SOON</h3>
</div>
Run Code Online (Sandbox Code Playgroud)

  • 您可以通过调整`.rainbow_text_animated` 动画的持续时间来自由改变速度。(比如将 `6s` 更改为 `12s` 以使慢度加倍)目前它正在将渐变背景向右移动,然后向左移动,然后向右移动等等。这会创建“循环”效果。 (2认同)