原创  一闪一闪小幽灵

分类:无    135人阅读    zhangenyu  2022-10-19 15:08

一闪一闪小幽灵

突发奇想做一个小幽灵的动画,用纯css就可以实现,主要是利用的css的关键帧动画@keyframes可以循环播放动画来实现的。

而html方面则是用div的多层嵌套实现的。

话不多说,直接看代码把,代码还是非常简单易懂的,相信又要有前端基础的人都能看得懂。

HTML

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>小幽灵</title>
  6. <link rel="stylesheet" href="/static/my/css/ghost.css">
  7. </head>
  8. <body>
  9. <div class='ghost-box'>
  10. <div class='symbol'></div>
  11. <div class='symbol'></div>
  12. <div class='symbol'></div>
  13. <div class='symbol'></div>
  14. <div class='symbol'></div>
  15. <div class='symbol'></div>
  16. <div class='ghost'>
  17. <div class='ghost-eyes'>
  18. <div class='eye-left'></div>
  19. <div class='eye-right'></div>
  20. </div>
  21. <div class='ghost-footer'>
  22. <div></div>
  23. <div></div>
  24. <div></div>
  25. <div></div>
  26. <div></div>
  27. </div>
  28. </div>
  29. <div class='ghost-shadow'></div>
  30. </div>
  31. </body>
  32. </html>

CSS

  1. @keyframes ghost-float {
  2. 0%,
  3. to {
  4. opacity: 0;
  5. transform: translateY(5px) scale(0.9);
  6. }
  7. 50% {
  8. opacity: 1;
  9. transform: translateY(25px) scale(1);
  10. }
  11. }
  12. @keyframes ghost-shadow {
  13. 0%,
  14. to {
  15. opacity: 0;
  16. transform: scale(0.8);
  17. }
  18. 50% {
  19. opacity: 1;
  20. transform: scale(1);
  21. }
  22. }
  23. @keyframes ghost-eyes {
  24. 0%,
  25. to {
  26. left: 0;
  27. }
  28. 40%,
  29. 60% {
  30. left: 30%;
  31. }
  32. }
  33. @keyframes shine {
  34. 0%,
  35. to {
  36. box-shadow: #fff 0 0 5px;
  37. opacity: 1;
  38. }
  39. 50% {
  40. box-shadow: #fff 0 0;
  41. opacity: 0;
  42. }
  43. }
  44. :root {
  45. --color-eyes: white;
  46. --color-body: #d4e8ee;
  47. --color-background: #131a24;
  48. --color-shadow: rgb(230, 211, 211);
  49. background-color: var(--color-background);
  50. }
  51. html {
  52. height: 100%;
  53. }
  54. body {
  55. height: 100%;
  56. display: flex;
  57. justify-content: center;
  58. align-items: center;
  59. }
  60. .ghost-box {
  61. padding: 15px 25px 25px;
  62. width: 150px;
  63. position: relative;
  64. box-sizing: border-box;
  65. * {
  66. box-sizing: border-box;
  67. }
  68. .ghost {
  69. background: var(--color-body);
  70. width: 100px;
  71. border-radius: 100px 100px 0 0;
  72. position: relative;
  73. margin: 0 auto;
  74. overflow: hidden;
  75. padding-bottom: 20px;
  76. height: 130px;
  77. background-clip: content-box;
  78. animation: ghost-float 8s ease-in-out infinite;
  79. .ghost-eyes {
  80. position: absolute;
  81. left: 50%;
  82. top: 30%;
  83. height: 12px;
  84. width: 70px;
  85. transition: all 0.05s ease-out;
  86. animation: ghost-eyes 5s ease-in-out infinite;
  87. .eye-left, .eye-right {
  88. width: 12px;
  89. height: 12px;
  90. background: var(--color-eyes);
  91. border-radius: 50%;
  92. margin: 0 10px;
  93. position: absolute;
  94. }
  95. .eye-left {
  96. left: 0;
  97. }
  98. .eye-right {
  99. right: 0;
  100. }
  101. }
  102. .ghost-footer {
  103. display: flex;
  104. position: absolute;
  105. left: 0;
  106. right: 0;
  107. bottom: 0;
  108. div {
  109. flex-grow: 1;
  110. position: relative;
  111. top: -10px;
  112. height: 20px;
  113. border-radius: 100%;
  114. background-color: var(--color-body);
  115. &:nth-child(2n) {
  116. top: -12px;
  117. margin: 0 0;
  118. border-top: 15px var(--color-background);
  119. background: transparent;
  120. }
  121. }
  122. }
  123. }
  124. .ghost-shadow {
  125. height: 20px;
  126. box-shadow: 0 50px 15px 5px var(--color-shadow);
  127. border-radius: 50%;
  128. margin: 0 auto;
  129. animation: ghost-shadow 8s ease-in-out infinite;
  130. }
  131. .symbol:nth-child(1) {
  132. opacity: 0.2;
  133. animation: shine 4s ease-in-out 3s infinite;
  134. }
  135. .symbol:nth-child(1):before, .symbol:nth-child(1):after {
  136. content: '';
  137. width: 12px;
  138. height: 4px;
  139. background: var(--color-body);
  140. position: absolute;
  141. border-radius: 5px;
  142. bottom: 65px;
  143. left: 0;
  144. }
  145. .symbol:nth-child(1):before {
  146. transform: rotate(45deg);
  147. }
  148. .symbol:nth-child(1):after {
  149. transform: rotate(-45deg);
  150. }
  151. .symbol:nth-child(2) {
  152. position: absolute;
  153. left: -5px;
  154. top: 30px;
  155. height: 18px;
  156. width: 18px;
  157. border: 4px solid;
  158. border-radius: 50%;
  159. border-color: var(--color-body);
  160. opacity: 0.2;
  161. animation: shine 4s ease-in-out 1.3s infinite;
  162. }
  163. .symbol:nth-child(3) {
  164. opacity: 0.2;
  165. animation: shine 3s ease-in-out 0.5s infinite;
  166. }
  167. .symbol:nth-child(3):before, .symbol:nth-child(3):after {
  168. content: '';
  169. width: 12px;
  170. height: 4px;
  171. background: var(--color-body);
  172. position: absolute;
  173. border-radius: 5px;
  174. top: 5px;
  175. left: 40px;
  176. }
  177. .symbol:nth-child(3):before {
  178. transform: rotate(90deg);
  179. }
  180. .symbol:nth-child(3):after {
  181. transform: rotate(180deg);
  182. }
  183. .symbol:nth-child(4) {
  184. opacity: 0.2;
  185. animation: shine 6s ease-in-out 1.6s infinite;
  186. }
  187. .symbol:nth-child(4):before, .symbol:nth-child(4):after {
  188. content: '';
  189. width: 15px;
  190. height: 4px;
  191. background: var(--color-body);
  192. position: absolute;
  193. border-radius: 5px;
  194. top: 10px;
  195. right: 30px;
  196. }
  197. .symbol:nth-child(4):before {
  198. transform: rotate(45deg);
  199. }
  200. .symbol:nth-child(4):after {
  201. transform: rotate(-45deg);
  202. }
  203. .symbol:nth-child(5) {
  204. position: absolute;
  205. right: 5px;
  206. top: 40px;
  207. height: 12px;
  208. width: 12px;
  209. border: 3px solid;
  210. border-radius: 50%;
  211. border-color: var(--color-body);
  212. opacity: 0.2;
  213. animation: shine 1.7s ease-in-out 7s infinite;
  214. }
  215. .symbol:nth-child(6) {
  216. opacity: 0.2;
  217. animation: shine 2s ease-in-out 6s infinite;
  218. }
  219. .symbol:nth-child(6):before, .symbol:nth-child(6):after {
  220. content: '';
  221. width: 15px;
  222. height: 4px;
  223. background: var(--color-body);
  224. position: absolute;
  225. border-radius: 5px;
  226. bottom: 65px;
  227. right: -5px;
  228. }
  229. .symbol:nth-child(6):before {
  230. transform: rotate(90deg);
  231. }
  232. .symbol:nth-child(6):after {
  233. transform: rotate(180deg);
  234. }
  235. }

这里的CSS我是用SCSS预编译的,所以需要大家先去安装SCSS才能使用

 

服务器费用不足...

给乌龟喂食卡通HTML5特效

HTML5大气导航栏鼠标悬停特效

JavaScript模拟网页星际旅行特效

Vue 3、Vite和TailwindCss开发的管理面板

bootstrap风格后台界面管理系统模板 - Voler

时尚的社交网站前端界面HTML模板 - Cirkle

CSS3机械工业风齿轮转动特效

建筑工程机械设备租赁网站HTML模板 - Antek

JS+CSS3卡通汽车行驶特效

一个alert网页小部件

时尚和轻量设计Bootstrap4管理系统模板 - Sunny

区块链数字货币管理系统网页模板 - Cryptio

Vue轻量级后台管理系统基础框架模板--精

物品租赁买卖业务平台HTML5模板 - Doremi

滚动下拉图片切斜HTML5特效

程序员向妹子表白专用代码

黑客帝国文字雨矩阵动画特效

UFO适合404页面的jQuery特效

Hotspot Map - 强大的图片热点注释和提示工具

bootstrap框架web UI工具包后台模板 - MegaDin

服务器费用不足...
 工具推荐 更多»