html – CSS水平线动画(渐变颜色)

前端之家收集整理的这篇文章主要介绍了html – CSS水平线动画(渐变颜色)前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有彩色,动画,垂直线这样一个:
  1. @keyframes colored {
  2. 0% {
  3. background-image: -webkit-linear-gradient(left,#c4e17f,#c4e17f 12.5%,#f7fdca 12.5%,#f7fdca 25%,#fecf71 25%,#fecf71 37.5%,#f0776c 37.5%,#f0776c 50%,#db9dbe 50%,#db9dbe 62.5%,#c49cde 62.5%,#c49cde 75%,#669ae1 75%,#669ae1 87.5%,#62c2e4 87.5%,#62c2e4);
  4. background-image: -moz-linear-gradient(left,#62c2e4);
  5. background-image: -o-linear-gradient(left,#62c2e4);
  6. background-image: linear-gradient(to right,#62c2e4);
  7. }
  8. 12.5% {
  9. background-image: -webkit-linear-gradient(left,#62c2e4,#62c2e4 12.5%,#c4e17f 25%,#f7fdca 37.5%,#fecf71 50%,#f0776c 62.5%,#db9dbe 75%,#c49cde 87.5%,#669ae1);
  10. background-image: -moz-linear-gradient(left,#669ae1);
  11. background-image: -o-linear-gradient(left,#669ae1);
  12. background-image: linear-gradient(to right,#669ae1);
  13. }
  14. 25% {
  15. background-image: -webkit-linear-gradient(left,#669ae1,#669ae1 12.5%,#62c2e4 25%,#c4e17f 37.5%,#f7fdca 50%,#fecf71 62.5%,#f0776c 75%,#db9dbe 87.5%,#c49cde);
  16. background-image: -moz-linear-gradient(left,#c49cde);
  17. background-image: -o-linear-gradient(left,#c49cde);
  18. background-image: linear-gradient(to right,#c49cde);
  19. }
  20. 37.5% {
  21. background-image: -webkit-linear-gradient(left,#c49cde,#c49cde 12.5%,#669ae1 25%,#62c2e4 37.5%,#c4e17f 50%,#f7fdca 62.5%,#fecf71 75%,#f0776c 87.5%,#db9dbe);
  22. background-image: -moz-linear-gradient(left,#db9dbe);
  23. background-image: -o-linear-gradient(left,#db9dbe);
  24. background-image: linear-gradient(to right,#db9dbe);
  25. }
  26. 50% {
  27. background-image: -webkit-linear-gradient(left,#db9dbe,#db9dbe 12.5%,#c49cde 25%,#669ae1 37.5%,#62c2e4 50%,#c4e17f 62.5%,#f7fdca 75%,#fecf71 87.5%,#f0776c);
  28. background-image: -moz-linear-gradient(left,#f0776c);
  29. background-image: -o-linear-gradient(left,#f0776c);
  30. background-image: linear-gradient(to right,#f0776c);
  31. }
  32. 62.5% {
  33. background-image: -webkit-linear-gradient(left,#f0776c,#f0776c 12.5%,#db9dbe 25%,#c49cde 37.5%,#669ae1 50%,#62c2e4 62.5%,#c4e17f 75%,#f7fdca 87.5%,#fecf71);
  34. background-image: -moz-linear-gradient(left,#fecf71);
  35. background-image: -o-linear-gradient(left,#fecf71);
  36. background-image: linear-gradient(to right,#fecf71);
  37. }
  38. 75% {
  39. background-image: -webkit-linear-gradient(left,#fecf71,#fecf71 12.5%,#f0776c 25%,#db9dbe 37.5%,#c49cde 50%,#669ae1 62.5%,#62c2e4 75%,#c4e17f 87.5%,#f7fdca);
  40. background-image: -moz-linear-gradient(left,#f7fdca);
  41. background-image: -o-linear-gradient(left,#f7fdca);
  42. background-image: linear-gradient(to right,#f7fdca);
  43. }
  44. 87.5% {
  45. background-image: -webkit-linear-gradient(left,#f7fdca,#c4e17f);
  46. background-image: -moz-linear-gradient(left,#c4e17f);
  47. background-image: -o-linear-gradient(left,#c4e17f);
  48. background-image: linear-gradient(to right,#c4e17f);
  49. }
  50. 100% {
  51. background-image: -webkit-linear-gradient(left,#62c2e4);
  52. }
  53. }
  54. .colored {
  55. margin-top: 5px;
  56. margin-bottom: 5px;
  57. height: 7px;
  58. border-top: 0;
  59. background: #c4e17f;
  60. border-radius: 5px;
  61. background-image: -webkit-linear-gradient(left,#62c2e4);
  62. background-image: -moz-linear-gradient(left,#62c2e4);
  63. background-image: -o-linear-gradient(left,#62c2e4);
  64. background-image: linear-gradient(to right,#62c2e4);
  65. animation-name: colored;
  66. animation-duration: 2s;
  67. animation-iteration-count: infinite;
  68. }
  1. <hr class="colored">

我想知道它是否可以在一些更好的(阅读:更聪明的方式)完成.花费我一些时间来计算和准备所有的动画步骤(正在使用excell),我觉得很愚蠢.

第二个问题(基于其中一个意见)是否这个动画可以更流畅?如何完成?

解决方法

保持结构最大化,这里是我的解决方
  1. .colored {
  2. margin-top: 5px;
  3. margin-bottom: 5px;
  4. height: 7px;
  5. border-top: 0;
  6. background: #c4e17f;
  7. border-radius: 5px;
  8. background-image: linear-gradient(to right,#62c2e4);
  9. padding-right: 50%;
  10. background-origin: content-Box;
  11. background-size: 200%;
  12. animation-name: colored;
  13. animation-duration: 2s;
  14. animation-iteration-count: infinite;
  15. animation-timing-function: steps(8);
  16. }
  17.  
  18. @keyframes colored {
  19. from {background-position: 0% 0%;}
  20. to {background-position: 200% 0%;}
  21. }
  1. <hr class="colored">

我用步骤功能来动画化背景位置,所以颜色的离散变化很容易实现.

使用一个单一的梯度,响应和动画有点棘手.

最简单的解决方案是什么,表示背景大小为100%,不受百分比的背景位置更改的影响.

诀窍是将背景设置为只有一半的元素(用填充技巧实现),然后将尺寸设置为200%

猜你在找的HTML相关文章