CSS3实现文字闪烁效果的多种形式代码

前端开发   发布日期:2023年05月22日   浏览次数:766

文字闪烁效果一

通过改变透明度来实现文字的渐变闪烁,代码如下:

  1. <div class="main">
  2. 文字闪烁:<span class="blink">闪烁效果</span></div><style type="text/css">.main{ color: #666;margin-top: 50px;
  3. }/* 定义keyframe动画,命名为blink */@keyframes blink{
  4. 0%{opacity: 1;}
  5. 100%{opacity: 0;}
  6. }/* 添加兼容性前缀 */@-webkit-keyframes blink {
  7. 0% { opacity: 1; }
  8. 100% { opacity: 0; }
  9. }
  10. @-moz-keyframes blink {
  11. 0% { opacity: 1; }
  12. 100% { opacity: 0; }
  13. }
  14. @-ms-keyframes blink {
  15. 0% {opacity: 1; }
  16. 100% { opacity: 0;}
  17. }
  18. @-o-keyframes blink {
  19. 0% { opacity: 1; }
  20. 100% { opacity: 0; }
  21. }/* 定义blink类*/.blink{ color: #dd4814; animation: blink 1s linear infinite;
  22. /* 其它浏览器兼容性前缀 */
  23. -webkit-animation: blink 1s linear infinite; -moz-animation: blink 1s linear infinite; -ms-animation: blink 1s linear infinite; -o-animation: blink 1s linear infinite;
  24. }</style>

如果不需要渐变闪烁效果,我们可以在keyframe动画中定义50%,50.1%的opacity的值。如下:

  1. @-webkit-keyframes blink {
  2. 0% { opacity: 1; }
  3. 50% { opacity: 1; }
  4. 50.01% { opacity: 0; }
  5. 100% { opacity: 0; }
  6. }

文字闪烁效果二

通过设置text-shadow的值,来实现文字阴影闪烁的效果,代码如下:

  1. <div class="box">闪烁效果</div><style>
  2. .box{
  3. font-size: 20px;
  4. color:#4cc134;
  5. margin: 10px; animation: changeshadow 1s ease-in infinite ; /* 其它浏览器兼容性前缀 */
  6. -webkit-animation: changeshadow 1s linear infinite; -moz-animation: changeshadow 1s linear infinite; -ms-animation: changeshadow 1s linear infinite; -o-animation: changeshadow 1s linear infinite;
  7. }
  8. @keyframes changeshadow {
  9. 0%{ text-shadow: 0 0 4px #4cc134}
  10. 50%{ text-shadow: 0 0 40px #4cc134}
  11. 100%{ text-shadow: 0 0 4px #4cc134}
  12. } /* 添加兼容性前缀 */
  13. @-webkit-keyframes changeshadow {
  14. 0%{ text-shadow: 0 0 4px #4cc134}
  15. 50%{ text-shadow: 0 0 40px #4cc134}
  16. 100%{ text-shadow: 0 0 4px #4cc134}
  17. }
  18. @-moz-keyframes changeshadow {
  19. 0%{ text-shadow: 0 0 4px #4cc134}
  20. 50%{ text-shadow: 0 0 40px #4cc134}
  21. 100%{ text-shadow: 0 0 4px #4cc134}
  22. }
  23. @-ms-keyframes changeshadow {
  24. 0%{ text-shadow: 0 0 4px #4cc134}
  25. 50%{ text-shadow: 0 0 40px #4cc134}
  26. 100%{ text-shadow: 0 0 4px #4cc134}
  27. }
  28. @-o-keyframes changeshadow {
  29. 0%{ text-shadow: 0 0 4px #4cc134}
  30. 50%{ text-shadow: 0 0 40px #4cc134}
  31. 100%{ text-shadow: 0 0 4px #4cc134}
  32. }</style>

文字闪烁效果三

利用背景图片或者背景渐变,实现文字颜色的闪烁效果,代码如下:

  1. <span class="box">闪烁效果</span><style>
  2. .box{
  3. display: inline-block; font-size: 20px; margin: 10px; background: linear-gradient(left, #f71605, #e0f513);
  4. background: -webkit-linear-gradient(left, #f71605, #e0f513); background: -o-linear-gradient(right, #f71605, #e0f513); -webkit-background-clip: text; -webkit-text-fill-color: transparent; animation:scratchy 0.253s linear forwards infinite; /* 其它浏览器兼容性前缀 */
  5. -webkit-animation:scratchy 0.253s linear forwards infinite; -moz-animation: scratchy 0.253s linear forwards infinite; -ms-animation: scratchy 0.253s linear forwards infinite; -o-animation: scratchy 0.253s linear forwards infinite;
  6. }
  7. @keyframes scratchy {
  8. 0% { background-position: 0 0;
  9. }
  10. 25% { background-position: 0 0;
  11. }
  12. 26% { background-position: 20px -20px;
  13. }
  14. 50% { background-position: 20px -20px;
  15. }
  16. 51% { background-position: 40px -40px;
  17. }
  18. 75% { background-position: 40px -40px;
  19. }
  20. 76% { background-position: 60px -60px;
  21. }
  22. 99% { background-position: 60px -60px;
  23. }
  24. 100% { background-position: 0 0;
  25. }
  26. } /* 添加兼容性前缀 */
  27. @-webkit-keyframes scratchy {
  28. 0% { background-position: 0 0;
  29. }
  30. 25% { background-position: 0 0;
  31. }
  32. 26% { background-position: 20px -20px;
  33. }
  34. 50% { background-position: 20px -20px;
  35. }
  36. 51% { background-position: 40px -40px;
  37. }
  38. 75% { background-position: 40px -40px;
  39. }
  40. 76% { background-position: 60px -60px;
  41. }
  42. 99% { background-position: 60px -60px;
  43. }
  44. 100% { background-position: 0 0;
  45. }
  46. }
  47. @-moz-keyframes scratchy {
  48. 0% { background-position: 0 0;
  49. }
  50. 25% { background-position: 0 0;
  51. }
  52. 26% { background-position: 20px -20px;
  53. }
  54. 50% { background-position: 20px -20px;
  55. }
  56. 51% { background-position: 40px -40px;
  57. }
  58. 75% { background-position: 40px -40px;
  59. }
  60. 76% { background-position: 60px -60px;
  61. }
  62. 99% { background-position: 60px -60px;
  63. }
  64. 100% { background-position: 0 0;
  65. }
  66. }
  67. @-ms-keyframes scratchy {
  68. 0% { background-position: 0 0;
  69. }
  70. 25% { background-position: 0 0;
  71. }
  72. 26% { background-position: 20px -20px;
  73. }
  74. 50% { background-position: 20px -20px;
  75. }
  76. 51% { background-position: 40px -40px;
  77. }
  78. 75% { background-position: 40px -40px;
  79. }
  80. 76% { background-position: 60px -60px;
  81. }
  82. 99% { background-position: 60px -60px;
  83. }
  84. 100% { background-position: 0 0;
  85. }
  86. }
  87. @-o-keyframes scratchy {
  88. 0% { background-position: 0 0;
  89. }
  90. 25% { background-position: 0 0;
  91. }
  92. 26% { background-position: 20px -20px;
  93. }
  94. 50% { background-position: 20px -20px;
  95. }
  96. 51% { background-position: 40px -40px;
  97. }
  98. 75% { background-position: 40px -40px;
  99. }
  100. 76% { background-position: 60px -60px;
  101. }
  102. 99% { background-position: 60px -60px;
  103. }
  104. 100% { background-position: 0 0;
  105. }
  106. }</style>

以上就是CSS3实现文字闪烁效果的多种形式代码的详细内容,更多关于CSS3实现文字闪烁效果的多种形式代码的资料请关注九品源码其它相关文章!