当前位置:   article > 正文

【前端】HTML+CSS实现卡通蜡烛动漫效果【附源码】

【前端】HTML+CSS实现卡通蜡烛动漫效果【附源码】

效果图:

         向大家介绍如何使用 CSS 和关键帧动画创建一个可爱的蜡烛动画效果。这个动画效果可以用于节日祝福、生日祝福等场合,让网页更加有趣和生动。

HTML代码

        

  1. <!doctype html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <title>纯css3卡通蜡烛小人动画特效演示</title>
  6. <link href="./css/datouwang.css" rel="stylesheet" type="text/css">
  7. </head>
  8. <body>
  9. <div class="wrapper">
  10. <div class="candles">
  11. <div class="light__wave"></div>
  12. <div class="candle1">
  13. <div class="candle1__body">
  14. <div class="candle1__eyes">
  15. <span class="candle1__eyes-one"></span>
  16. <span class="candle1__eyes-two"></span>
  17. </div>
  18. <div class="candle1__mouth"></div>
  19. </div>
  20. <div class="candle1__stick"></div>
  21. </div>
  22. <div class="candle2">
  23. <div class="candle2__body">
  24. <div class="candle2__eyes">
  25. <div class="candle2__eyes-one"></div>
  26. <div class="candle2__eyes-two"></div>
  27. </div>
  28. </div>
  29. <div class="candle2__stick"></div>
  30. </div>
  31. <div class="candle2__fire"></div>
  32. <div class="sparkles-one"></div>
  33. <div class="sparkles-two"></div>
  34. <div class="candle__smoke-one"></div>
  35. <div class="candle__smoke-two"></div>
  36. </div>
  37. <div class="floor"></div>
  38. </div>
  39. </body>
  40. </html>

<link>标签:


    通过<link>标签引入外部CSS文件datouwang.css,该文件包含了样式定义用于渲染页面元素。

页面结构:

  .wrapper:整体容器,用于包裹所有元素。
    .candles:蜡烛小人容器,包括蜡烛、火焰、眼睛、嘴巴等元素。
        .light__wave:光波效果。
        .candle1和.candle2:两根蜡烛,每根蜡烛包括蜡烛身体、眼睛、眼睛、眼睛、火焰以及支架等部分。
            .candle1__body和.candle2__body:蜡烛的身体部分。
            .candle1__eyes和.candle2__eyes:蜡烛的眼睛部分。
                .candle1__eyes-one、.candle1__eyes-two、.candle2__eyes-one、.candle2__eyes-two:眼睛的具体元素。
            .candle1__mouth:蜡烛1的嘴巴部分。
            .candle1__stick和.candle2__stick:蜡烛的支架部分。
        .candle2__fire:蜡烛2的火焰部分。
        .sparkles-one和.sparkles-two:闪烁效果
        .candle__smoke-one和.candle__smoke-two:烟雾效果。

 floor:地板部分

CSS结构:

  1. body 部分设置了背景颜色以及一个名为 change-background 的动画,使背景颜色在两种不同的颜色之间循环变化。

  2. .wrapper 是整个动画的容器,通过绝对定位将其放置在页面的中心位置,并进行缩放和偏移。

  3. .floor 是一个代表地面的元素,也进行了绝对定位,并设置了阴影效果。

  4. .candles 是蜡烛的容器,用于包裹所有蜡烛元素。

  5. .candle1.candle2 分别代表两支蜡烛,它们都是绝对定位的白色长方体,通过设置不同的动画效果,让它们看起来像是在跳舞或者闪烁。

  6. .candle1__stick.candle2__stick 是蜡烛的支架,使用绝对定位放置并进行了旋转和移动的动画效果。

  7. .candle1__eyes, .candle2__eyes, .candle1__mouth 以及 .candle__smoke-one, .candle__smoke-two 等元素则是组成蜡烛的眼睛、嘴巴和烟雾等部分,通过不同的动画效果来模拟眨眼、摇晃和吐烟的效果。

  8. 最后还有一些关键帧动画 (@keyframes) 的定义,用于实现上述提到的各种动画效果,比如眨眼、身体放大缩小、火焰舞动等。

  1. body {
  2. background-color: #FEF4AD;
  3. animation: change-background 3s infinite linear;
  4. }
  5. .wrapper {
  6. position: absolute;
  7. left: 50%;
  8. top: 70%;
  9. transform: scale(1.5, 1.5) translate(-50%, -50%);
  10. }
  11. .floor {
  12. position: absolute;
  13. left: 50%;
  14. top: 50%;
  15. width: 350px;
  16. height: 5px;
  17. background: #673C63;
  18. transform: translate(-50%, -50%);
  19. box-shadow: 0px 2px 5px #111;
  20. z-index: 2;
  21. }
  22. .candles {
  23. position: absolute;
  24. left: 50%;
  25. top: 50%;
  26. width: 250px;
  27. height: 150px;
  28. transform: translate(-50%, -100%);
  29. z-index: 1;
  30. }
  31. .candle1 {
  32. position: absolute;
  33. left: 50%;
  34. top: 50%;
  35. width: 35px;
  36. height: 100px;
  37. background: #fff;
  38. border: 3px solid #673C63;
  39. border-bottom: 0px;
  40. border-radius: 3px;
  41. transform-origin: center right;
  42. transform: translate(60%, -25%);
  43. box-shadow: -2px 0px 0px #95c6f2 inset;
  44. animation: expand-body 3s infinite linear;
  45. }
  46. .candle1__stick, .candle2__stick {
  47. position: absolute;
  48. left: 50%;
  49. top: 0%;
  50. width: 3px;
  51. height: 15px;
  52. background: #673C63;
  53. border-radius: 8px;
  54. transform: translate(-50%, -100%);
  55. }
  56. .candle2__stick {
  57. height: 12px;
  58. transform-origin: bottom center;
  59. animation: stick-animation 3s infinite linear;
  60. }
  61. .candle1__eyes, .candle2__eyes {
  62. position: absolute;
  63. left: 50%;
  64. top: 0%;
  65. width: 35px;
  66. height: 30px;
  67. transform: translate(-50%, 0%);
  68. }
  69. .candle1__eyes-one {
  70. position: absolute;
  71. left: 30%;
  72. top: 20%;
  73. width: 5px;
  74. height: 5px;
  75. border-radius: 100%;
  76. background: #673C63;
  77. transform: translate(-70%, 0%);
  78. animation: blink-eyes 3s infinite linear;
  79. }
  80. .candle1__eyes-two {
  81. position: absolute;
  82. left: 70%;
  83. top: 20%;
  84. width: 5px;
  85. height: 5px;
  86. border-radius: 100%;
  87. background: #673C63;
  88. transform: translate(-70%, 0%);
  89. animation: blink-eyes 3s infinite linear;
  90. }
  91. .candle1__mouth {
  92. position: absolute;
  93. left: 40%;
  94. top: 20%;
  95. width: 0px;
  96. height: 0px;
  97. border-radius: 20px;
  98. background: #673C63;
  99. transform: translate(-50%, -50%);
  100. animation: uff 3s infinite linear;
  101. }
  102. .candle__smoke-one {
  103. position: absolute;
  104. left: 30%;
  105. top: 50%;
  106. width: 30px;
  107. height: 3px;
  108. background: grey;
  109. transform: translate(-50%, -50%);
  110. animation: move-left 3s infinite linear;
  111. }
  112. .candle__smoke-two {
  113. position: absolute;
  114. left: 30%;
  115. top: 40%;
  116. width: 10px;
  117. height: 10px;
  118. border-radius: 10px;
  119. background: grey;
  120. transform: translate(-50%, -50%);
  121. animation: move-top 3s infinite linear;
  122. }
  123. .candle2 {
  124. position: absolute;
  125. left: 20%;
  126. top: 65%;
  127. width: 42px;
  128. height: 60px;
  129. background: #fff;
  130. border: 3px solid #673C63;
  131. border-bottom: 0px;
  132. border-radius: 3px;
  133. transform: translate(60%, -15%);
  134. transform-origin: center right;
  135. box-shadow: -2px 0px 0px #95c6f2 inset;
  136. animation: shake-left 3s infinite linear;
  137. }
  138. .candle2__eyes-one {
  139. position: absolute;
  140. left: 30%;
  141. top: 50%;
  142. width: 5px;
  143. height: 5px;
  144. display: inline-block;
  145. border: 0px solid #673C63;
  146. border-radius: 100%;
  147. float: left;
  148. background: #673C63;
  149. transform: translate(-80%, 0%);
  150. animation: changeto-lower 3s infinite linear;
  151. }
  152. .candle2__eyes-two {
  153. position: absolute;
  154. left: 70%;
  155. top: 50%;
  156. width: 5px;
  157. height: 5px;
  158. display: inline-block;
  159. border: 0px solid #673C63;
  160. border-radius: 100%;
  161. float: left;
  162. background: #673C63;
  163. transform: translate(-80%, 0%);
  164. animation: changeto-greater 3s infinite linear;
  165. }
  166. .light__wave {
  167. position: absolute;
  168. top: 35%;
  169. left: 35%;
  170. width: 75px;
  171. height: 75px;
  172. border-radius: 100%;
  173. z-index: 0;
  174. transform: translate(-25%, -50%) scale(2.5, 2.5);
  175. border: 2px solid rgba(255, 255, 255, 0.2);
  176. animation: expand-light 3s infinite linear;
  177. }
  178. .candle2__fire {
  179. position: absolute;
  180. top: 50%;
  181. left: 40%;
  182. display: block;
  183. width: 16px;
  184. height: 20px;
  185. background-color: red;
  186. border-radius: 50% 50% 50% 50% / 60% 60% 40% 40%;
  187. background: #FF9800;
  188. transform: translate(-50%, -50%);
  189. animation: dance-fire 3s infinite linear;
  190. }
  191. @keyframes blink-eyes {
  192. 0%,35% {
  193. opacity: 1;
  194. transform: translate(-70%, 0%);
  195. }
  196. 36%,39% {
  197. opacity: 0;
  198. transform: translate(-70%, 0%);
  199. }
  200. 40% {
  201. opacity: 1;
  202. transform: translate(-70%, 0%);
  203. }
  204. 50%,65% {
  205. transform: translate(-140%, 0%);
  206. }
  207. 66% {
  208. transform: translate(-70%, 0%);
  209. }
  210. }
  211. @keyframes expand-body {
  212. 0%,40% {
  213. transform: scale(1, 1) translate(60%, -25%);
  214. }
  215. 45%,55% {
  216. transform: scale(1.1, 1.1) translate(60%, -28%);
  217. }
  218. 60% {
  219. transform: scale(0.89, 0.89) translate(60%, -25%);
  220. }
  221. 65% {
  222. transform: scale(1, 1) translate(60%, -25%);
  223. }
  224. 70% {
  225. transform: scale(0.95, 0.95) translate(60%, -25%);
  226. }
  227. 75% {
  228. transform: scale(1, 1) translate(60%, -25%);
  229. }
  230. }
  231. @keyframes uff {
  232. 0%,40% {
  233. width: 0px;
  234. height: 0px;
  235. }
  236. 50%,54% {
  237. width: 15px;
  238. height: 15px;
  239. left: 30%;
  240. }
  241. 59% {
  242. width: 5px;
  243. height: 5px;
  244. left: 20%;
  245. }
  246. 62% {
  247. width: 2px;
  248. height: 2px;
  249. left: 20%;
  250. }
  251. 67% {
  252. width: 0px;
  253. height: 0px;
  254. left: 30%;
  255. }
  256. }
  257. @keyframes change-background {
  258. 0%,59%,98%,100% {
  259. background: #FEF4AD;
  260. }
  261. 61%,97% {
  262. background: #F8AE39;
  263. }
  264. }
  265. @keyframes move-left {
  266. 0%,59%,100% {
  267. width: 0px;
  268. left: 40%;
  269. }
  270. 60% {
  271. width: 30px;
  272. left: 30%;
  273. }
  274. 68% {
  275. width: 0px;
  276. left: 20%;
  277. }
  278. }
  279. @keyframes move-top {
  280. 0%,64%,100% {
  281. width: 0px;
  282. height: 0px;
  283. top: 0%;
  284. }
  285. 65% {
  286. width: 10px;
  287. height: 10px;
  288. top: 40%;
  289. left: 40%;
  290. }
  291. 80% {
  292. width: 0px;
  293. height: 0px;
  294. top: 20%;
  295. }
  296. }
  297. @keyframes shake-left {
  298. 0%,40% {
  299. left: 20%;
  300. transform: translate(60%, -15%);
  301. }
  302. 50%,54% {
  303. left: 20%;
  304. transform: translate(60%, -15%);
  305. }
  306. 59% {
  307. left: 20%;
  308. transform: translate(60%, -15%);
  309. }
  310. 62% {
  311. left: 18%;
  312. transform: translate(60%, -15%);
  313. }
  314. 65% {
  315. left: 21%;
  316. transform: translate(60%, -15%);
  317. }
  318. 67% {
  319. left: 20%;
  320. transform: translate(60%, -15%);
  321. }
  322. 75% {
  323. left: 20%;
  324. transform: scale(1.15, 0.85) translate(60%, -15%);
  325. background: #fff;
  326. border-color: #673C63;
  327. }
  328. 91% {
  329. left: 20%;
  330. transform: scale(1.18, 0.82) translate(60%, -10%);
  331. background: #F44336;
  332. border-color: #F44336;
  333. box-shadow: -2px 0px 0px #F44336 inset;
  334. }
  335. 92% {
  336. left: 20%;
  337. transform: scale(0.85, 1.15) translate(60%, -15%);
  338. }
  339. 95% {
  340. left: 20%;
  341. transform: scale(1.05, 0.95) translate(60%, -15%);
  342. }
  343. 97% {
  344. left: 20%;
  345. transform: scale(1, 1) translate(60%, -15%);
  346. }
  347. }
  348. @keyframes stick-animation {
  349. 0%,40% {
  350. left: 50%;
  351. top: 0%;
  352. transform: translate(-50%, -100%);
  353. }
  354. 50%,54% {
  355. left: 50%;
  356. top: 0%;
  357. transform: translate(-50%, -100%);
  358. }
  359. 59% {
  360. left: 50%;
  361. top: 0%;
  362. transform: translate(-50%, -100%);
  363. }
  364. 62% {
  365. left: 50%;
  366. top: 0%;
  367. transform: rotateZ(-15deg) translate(-50%, -100%);
  368. }
  369. 65% {
  370. left: 50%;
  371. top: 0%;
  372. transform: rotateZ(15deg) translate(-50%, -100%);
  373. }
  374. 70% {
  375. left: 50%;
  376. top: 0%;
  377. transform: rotateZ(-5deg) translate(-50%, -100%);
  378. }
  379. 72% {
  380. left: 50%;
  381. top: 0%;
  382. transform: rotateZ(5deg) translate(-50%, -100%);
  383. }
  384. 74%,84% {
  385. left: 50%;
  386. top: 0%;
  387. transform: rotateZ(0deg) translate(-50%, -100%);
  388. }
  389. 85% {
  390. transform: rotateZ(180deg) translate(0%, 120%);
  391. }
  392. 92% {
  393. left: 50%;
  394. top: 0%;
  395. transform: translate(-50%, -100%);
  396. }
  397. }
  398. @keyframes expand-light {
  399. 10%,29%,59%,89% {
  400. transform: translate(-25%, -50%) scale(0, 0);
  401. border: 2px solid rgba(255, 255, 255, 0);
  402. }
  403. 90%,20%,50% {
  404. transform: translate(-25%, -50%) scale(1, 1);
  405. }
  406. 95%,96%,26%,27%,56%,57% {
  407. transform: translate(-25%, -50%) scale(2, 2);
  408. border: 2px solid rgba(255, 255, 255, 0.5);
  409. }
  410. 0%,28%,58%,100% {
  411. transform: translate(-25%, -50%) scale(2.5, 2.5);
  412. border: 2px solid rgba(255, 255, 255, 0.2);
  413. }
  414. }
  415. @keyframes dance-fire {
  416. 59%,89% {
  417. left: 40%;
  418. width: 0px;
  419. height: 0px;
  420. }
  421. 90%,0%,7%,15%,23%,31%,39%,47%,55% {
  422. left: 40.8%;
  423. width: 16px;
  424. height: 20px;
  425. background: #FFC107;
  426. }
  427. 94%,3%,11%,19%,27%,35%,43%,51%,58% {
  428. left: 41.2%;
  429. width: 16px;
  430. height: 20px;
  431. background: #FF9800;
  432. }
  433. }
  434. @keyframes changeto-lower {
  435. 0%,70%,90% {
  436. padding: 0px;
  437. display: inline-block;
  438. border-radius: 100%;
  439. background: #673C63;
  440. border-width: 0 0 0 0;
  441. border: 0px solid #673C63;
  442. transform: translate(-90%, 0%);
  443. }
  444. 71%,89% {
  445. background: none;
  446. border: solid #673C63;
  447. border-radius: 0px;
  448. border-width: 0 2px 2px 0;
  449. display: inline-block;
  450. padding: 1px;
  451. float: left;
  452. transform-origin: bottom left;
  453. transform: rotate(-45deg) translate(-50%, -65%);
  454. -webkit-transform: rotate(-45deg) translate(-50%, -65%);
  455. }
  456. }
  457. @keyframes changeto-greater {
  458. 0%,70%,90% {
  459. top: 50%;
  460. padding: 0px;
  461. display: inline-block;
  462. border-radius: 100%;
  463. background: #673C63;
  464. border-width: 0 0 0 0;
  465. border: 0px solid #673C63;
  466. transform: translate(-80%, 0%);
  467. }
  468. 71%,89% {
  469. top: 30%;
  470. background: none;
  471. border: solid #673C63;
  472. border-radius: 0px;
  473. border-width: 0 2px 2px 0;
  474. display: inline-block;
  475. padding: 1px;
  476. float: left;
  477. transform-origin: bottom left;
  478. transform: rotate(135deg) translate(-80%, 20%);
  479. -webkit-transform: rotate(135deg) translate(-80%, 20%);
  480. }
  481. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/菜鸟追梦旅行/article/detail/244433
推荐阅读
相关标签
  

闽ICP备14008679号