如何使用纯CSS实现彩虹条纹文字的效果(附代码)

本篇文章给大家带来的内容是关于如何使用纯css实现彩虹条纹文字的效果(附代码),有一定的参考价值,有需要的朋友可以参考一下,希望对你有所帮助。

效果预览

1960798176-5b0f3e71e4355_articlex.png

源代码下载

https://github.com/comehope/front-end-daily-challenges

代码解读

定义 dom,容器中包含文本,并且包含4个 用于特效, 的 data-text 属性值为与文本相同:

web

登录后复制

居中显示:

立即学习“前端免费学习笔记(深入)”;

html, body {    height: 100%;    display: flex;    align-items: center;    justify-content: center;    background: black;}

登录后复制

定义文本样式:

.rainbow {    color: white;    font-size: 300px;    text-transform: uppercase;    font-family: sans-serif;    font-weight: bold;    line-height: 1em;    position: relative;}

登录后复制

用伪元素增加图层,形成彩虹效果:

.rainbow span::before,.rainbow span::after {    content: attr(data-text);    position: absolute;    top: 0;    left: 0;    overflow: hidden;}.rainbow span:nth-child(1)::before {    color: orchid;    z-index: 1;    height: calc(100% - 10% * 1);}.rainbow span:nth-child(1)::after {    color: mediumpurple;    z-index: 2;    height: calc(100% - 10% * 2);}.rainbow span:nth-child(2)::before {    color: deepskyblue;    z-index: 3;    height: calc(100% - 10% * 3);}.rainbow span:nth-child(2)::after {    color: cyan;    z-index: 4;    height: calc(100% - 10% * 4);}.rainbow span:nth-child(3)::before {    color: mediumspringgreen;    z-index: 5;    height: calc(100% - 10% * 5);}.rainbow span:nth-child(3)::after {    color: yellow;    z-index: 6;    height: calc(100% - 10% * 6);}.rainbow span:nth-child(4)::before {    color: gold;    z-index: 7;    height: calc(100% - 10% * 7);}.rainbow span:nth-child(4)::after {    color: tomato;    z-index: 8;    height: calc(100% - 10% * 8);}

登录后复制

增加动画效果:

.rainbow span::before,.rainbow span::after {    animation: animate 0.8s infinite alternate;    filter: opacity(0);}@keyframes animate {    from {        filter: opacity(0);    }    to {        filter: opacity(1);    }}

登录后复制

为图层设置延时,增强动感:

.rainbow span:nth-child(1)::before {    animation-delay: calc(0.8s - 0.1s * 1);}.rainbow span:nth-child(1)::after {    animation-delay: calc(0.8s - 0.1s * 2);}.rainbow span:nth-child(2)::before {    animation-delay: calc(0.8s - 0.1s * 3);}.rainbow span:nth-child(2)::after {    animation-delay: calc(0.8s - 0.1s * 4);}.rainbow span:nth-child(3)::before {    animation-delay: calc(0.8s - 0.1s * 5);}.rainbow span:nth-child(3)::after {    animation-delay: calc(0.8s - 0.1s * 6);}.rainbow span:nth-child(4)::before {    animation-delay: calc(0.8s - 0.1s * 7);}.rainbow span:nth-child(4)::after {    animation-delay: calc(0.8s - 0.1s * 8);}

登录后复制

最后,把原始文本设置为透明色:

.rainbow {    color: transparent;}

登录后复制

大功告成!

相关推荐:

如何使用纯CSS实现冰棍的动画效果(附代码)

如何使用纯CSS实现冰棍的动画效果(附代码)

以上就是如何使用纯CSS实现彩虹条纹文字的效果(附代码)的详细内容,更多请关注【创想鸟】其它相关文章!

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至253000106@qq.com举报,一经查实,本站将立刻删除。

发布者:PHP中文网,转转请注明出处:https://www.chuangxiangniao.com/p/2893423.html

(0)
上一篇 2025年3月10日 22:35:30
下一篇 2025年3月8日 01:19:03

AD推荐 黄金广告位招租... 更多推荐

相关推荐

发表回复

登录后才能评论