CSS 卡通动画

HTML代码

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content=
  6. "width=device-width, initial-scale=1.0">
  7. <title>Animated loader</title>
  8. <link rel="stylesheet" href="style.css">
  9. </head>
  10. <body>
  11. <div class="pieces"></div>
  12. <div class="one"></div>
  13. <div class="two"></div>
  14. <div class="eye"></div>
  15. <p>GeeksforGeeks loading...</p>
  16. </body>
  17. </html>

CSS代码

卡通加载器的设计是通过CSS属性实现的。
通过逐渐从一组样式更改为另一组样式来创建动画。更改以百分比或关键字“ from”和“ to”为单位,分别与0%和100%相同。
可以根据需要多次更改CSS样式集。

  1. @keyframes animationname {keyframes-selector {css-styles;}}
  1. body{
  2. display: flex;
  3. justify-content: center;
  4. align-items: center;
  5. min-height: 100vh;
  6. background: #202020;
  7. }
  8. .pieces{
  9. padding: 10px;
  10. border-radius: 50%;
  11. background: #ffcc00;
  12. position: absolute;
  13. right: 40%;
  14. animation: pieces 1s linear infinite;
  15. }
  16. .one{
  17. position: absolute;
  18. top: 50.5%;
  19. left: 30%;
  20. background: yellowgreen;
  21. border-radius: 0 0 125px 125px;
  22. height: 125px;
  23. width: 250px;
  24. animation: anim1 1s linear infinite;
  25. }
  26. .two{
  27. position: absolute;
  28. top: 36.5%;
  29. left: 30%;
  30. background: yellowgreen;
  31. border-radius: 125px 125px 0 0;
  32. height: 125px;
  33. width: 250px;
  34. animation: anim2 1s linear infinite;
  35. }
  36. .eye{
  37. position: absolute;
  38. right: 60%;
  39. top: 40%;
  40. background: #202020;
  41. padding: 12px;
  42. border-radius: 50%;
  43. animation: eye 1s linear infinite;
  44. }
  45. p{
  46. position: absolute;
  47. font-weight: bold;
  48. text-transform: uppercase;
  49. font-size: 25px;
  50. letter-spacing: 2px;
  51. top: 53%;
  52. right: 30%;
  53. font-family: arial;
  54. color: green;
  55. }
  56. @keyframes anim1{
  57. 0%{
  58. transform: rotate(0deg);
  59. }
  60. 50%{
  61. transform: rotate(20deg);
  62. }
  63. 100%{
  64. transform: rotate(0deg);
  65. }
  66. }
  67. @keyframes anim2{
  68. 0%{
  69. transform: rotate(0deg);
  70. }
  71. 50%{
  72. transform: rotate(-20deg);
  73. }
  74. 100%{
  75. transform: rotate(0deg);
  76. }
  77. }
  78. @keyframes eye{
  79. 0%{
  80. top: 40%;
  81. right: 60%;
  82. }
  83. 50%{
  84. top: 40.3%;
  85. right: 60.3%;
  86. }
  87. 100%{
  88. top: 40%;
  89. right: 60%;
  90. }
  91. }
  92. @keyframes pieces{
  93. 0%{
  94. right: 40%;
  95. }
  96. 100%{
  97. right: 60%;
  98. }
  99. }

完整代码

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content=
  6. "width=device-width, initial-scale=1.0">
  7. <title>Animated loader</title>
  8. <style>
  9. body {
  10. display: flex;
  11. justify-content: center;
  12. align-items: center;
  13. min-height: 100vh;
  14. background: #202020;
  15. }
  16. .pieces {
  17. padding: 10px;
  18. border-radius: 50%;
  19. background: #ffcc00;
  20. position: absolute;
  21. right: 40%;
  22. animation: pieces 1s linear infinite;
  23. }
  24. .one {
  25. position: absolute;
  26. top: 50.5%;
  27. left: 30%;
  28. background: yellowgreen;
  29. border-radius: 0 0 125px 125px;
  30. height: 125px;
  31. width: 250px;
  32. animation: anim1 1s linear infinite;
  33. }
  34. .two {
  35. position: absolute;
  36. top: 36.5%;
  37. left: 30%;
  38. background: yellowgreen;
  39. border-radius: 125px 125px 0 0;
  40. height: 125px;
  41. width: 250px;
  42. animation: anim2 1s linear infinite;
  43. }
  44. .eye {
  45. position: absolute;
  46. right: 60%;
  47. top: 40%;
  48. background: #202020;
  49. padding: 12px;
  50. border-radius: 50%;
  51. animation: eye 1s linear infinite;
  52. }
  53. p {
  54. position: absolute;
  55. font-weight: bold;
  56. text-transform: uppercase;
  57. font-size: 25px;
  58. letter-spacing: 2px;
  59. top: 53%;
  60. right: 30%;
  61. font-family: arial;
  62. color: green;
  63. }
  64. @keyframes anim1 {
  65. 0% {
  66. transform: rotate(0deg);
  67. }
  68. 50% {
  69. transform: rotate(20deg);
  70. }
  71. 100% {
  72. transform: rotate(0deg);
  73. }
  74. }
  75. @keyframes anim2 {
  76. 0% {
  77. transform: rotate(0deg);
  78. }
  79. 50% {
  80. transform: rotate(-20deg);
  81. }
  82. 100% {
  83. transform: rotate(0deg);
  84. }
  85. }
  86. @keyframes eye {
  87. 0% {
  88. top: 40%;
  89. right: 60%;
  90. }
  91. 50% {
  92. top: 40.3%;
  93. right: 60.3%;
  94. }
  95. 100% {
  96. top: 40%;
  97. right: 60%;
  98. }
  99. }
  100. @keyframes pieces {
  101. 0% {
  102. right: 40%;
  103. }
  104. 100% {
  105. right: 60%;
  106. }
  107. }
  108. </style>
  109. </head>
  110. <body>
  111. <div class="pieces"></div>
  112. <div class="one"></div>
  113. <div class="two"></div>
  114. <div class="eye"></div>
  115. <p>GeeksforGeeks loading...</p>
  116. </body>
  117. </html>

实现效果

GIF 2020-9-8-星期二 12-23-45.gif