温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

怎么使用纯CSS实现彩虹条纹文字的效果

发布时间:2022-02-28 13:56:22 来源:亿速云 阅读:183 作者:小新 栏目:web开发

小编给大家分享一下怎么使用纯CSS实现彩虹条纹文字的效果,希望大家阅读完这篇文章之后都有所收获,下面让我们一起去探讨吧!

  代码解读

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

  <pclass="rainbow">

  web

  <spandata-text="web"></span>

  <spandata-text="web"></span>

  <spandata-text="web"></span>

  <spandata-text="web"></span>

  </p>

  居中显示:

  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;

  }

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

  .rainbowspan::before,

  .rainbowspan::after{

  content:attr(data-text);

  position:absolute;

  top:0;

  left:0;

  overflow:hidden;

  }

  .rainbowspan:nth-child(1)::before{

  color:orchid;

  z-index:1;

  height:calc(100%-10%*1);

  }

  .rainbowspan:nth-child(1)::after{

  color:mediumpurple;

  z-index:2;

  height:calc(100%-10%*2);

  }

  .rainbowspan:nth-child(2)::before{

  color:deepskyblue;

  z-index:3;

  height:calc(100%-10%*3);

  }

  .rainbowspan:nth-child(2)::after{

  color:cyan;

  z-index:4;

  height:calc(100%-10%*4);

  }

  .rainbowspan:nth-child(3)::before{

  color:mediumspringgreen;

  z-index:5;

  height:calc(100%-10%*5);

  }

  .rainbowspan:nth-child(3)::after{

  color:yellow;

  z-index:6;

  height:calc(100%-10%*6);

  }

  .rainbowspan:nth-child(4)::before{

  color:gold;

  z-index:7;

  height:calc(100%-10%*7);

  }

  .rainbowspan:nth-child(4)::after{

  color:tomato;

  z-index:8;

  height:calc(100%-10%*8);

  }

  增加动画效果:

  .rainbowspan::before,

  .rainbowspan::after{

  animation:animate0.8sinfinitealternate;

  filter:opacity(0);

  }

  @keyframesanimate{

  from{

  filter:opacity(0);

  }

  to{

  filter:opacity(1);

  }

  }

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

  .rainbowspan:nth-child(1)::before{

  animation-delay:calc(0.8s-0.1s*1);

  }

  .rainbowspan:nth-child(1)::after{

  animation-delay:calc(0.8s-0.1s*2);

  }

  .rainbowspan:nth-child(2)::before{

  animation-delay:calc(0.8s-0.1s*3);

  }

  .rainbowspan:nth-child(2)::after{

  animation-delay:calc(0.8s-0.1s*4);

  }

  .rainbowspan:nth-child(3)::before{

  animation-delay:calc(0.8s-0.1s*5);

  }

  .rainbowspan:nth-child(3)::after{

  animation-delay:calc(0.8s-0.1s*6);

  }

  .rainbowspan:nth-child(4)::before{

  animation-delay:calc(0.8s-0.1s*7);

  }

  .rainbowspan:nth-child(4)::after{

  animation-delay:calc(0.8s-0.1s*8);

  }

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

  .rainbow{

  color:transparent;

  }

看完了这篇文章,相信你对“怎么使用纯CSS实现彩虹条纹文字的效果”有了一定的了解,如果想了解更多相关知识,欢迎关注亿速云行业资讯频道,感谢各位的阅读!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

css
AI