当前位置:Gxlcms > css > 如何使用纯CSS实现蚊香燃烧的效果(附源码)

如何使用纯CSS实现蚊香燃烧的效果(附源码)

时间:2021-07-01 10:21:17 帮助过:34人阅读

本篇文章给大家带来的内容是关于如何使用纯CSS实现传统蚊香燃烧的效果(附源码),有一定的参考价值,有需要的朋友可以参考一下,希望对你有所帮助。

效果预览

3235001503-5b1ddfb66be51_articlex.png

源代码下载

每日前端实战系列的全部源代码请从 github 下载:

https://github.com/comehope/front-end-daily-challenges

代码解读

定义 dom,容器中包含 8 个子元素:

  1. <div class="coil">
  2. <span></span>
  3. <span></span>
  4. <span></span>
  5. <span></span>
  6. <span></span>
  7. <span></span>
  8. <span></span>
  9. <span></span>
  10. </div>

居中显示:

  1. body {
  2. margin: 0;
  3. height: 100vh;
  4. display: flex;
  5. align-items: center;
  6. justify-content: center;
  7. background: radial-gradient(circle at center, midnightblue, black);
  8. }

画出纹香盘要用的框线:

  1. .coil {
  2. position: relative;
  3. display: flex;
  4. justify-content: center;
  5. }
  6. .coil span {
  7. position: absolute;
  8. width: calc((var(--n) * 2 - 1) * 1em);
  9. height: calc((var(--n) - 0.5) * 1em);
  10. border: 1em solid darkgreen;
  11. }
  12. .coil span:nth-child(1) {
  13. --n: 1;
  14. }
  15. .coil span:nth-child(2) {
  16. --n: 2;
  17. }
  18. .coil span:nth-child(3) {
  19. --n: 3;
  20. }
  21. .coil span:nth-child(4) {
  22. --n: 4;
  23. }
  24. .coil span:nth-child(5) {
  25. --n: 5;
  26. }
  27. .coil span:nth-child(6) {
  28. --n: 6;
  29. }
  30. .coil span:nth-child(7) {
  31. --n: 7;
  32. }
  33. .coil span:nth-child(8) {
  34. --n: 8;
  35. }

把一半框线放置到上方:

  1. .coil span:nth-child(odd) {
  2. align-self: flex-end;
  3. }

删除掉上方框线的下边框,和下方框线的上边框:

  1. .coil span:nth-child(odd) {
  2. border-bottom: none;
  3. }
  4. .coil span:nth-child(even) {
  5. border-top: none;
  6. }

对齐上下边框:

  1. .coil span:nth-child(even) {
  2. transform: translateX(-1em);
  3. }

把边框改为曲线:

  1. .coil span:nth-child(odd) {
  2. border-radius: 50% 50% 0 0 / 100% 100% 0 0;
  3. }
  4. .coil span:nth-child(even) {
  5. border-radius: 0 0 50% 50% / 0 0 100% 100%;
  6. }

用伪元素画出蚊香最中间的部分:

  1. .coil::before {
  2. content: '';
  3. position: absolute;
  4. width: 1em;
  5. height: 1em;
  6. background-color: darkgreen;
  7. border-radius: 50%;
  8. left: -1.5em;
  9. top: -0.5em;
  10. }

用伪元素画出蚊香的燃点:

  1. .coil::after {
  2. content: '';
  3. position: absolute;
  4. width: 1em;
  5. height: 1em;
  6. border-radius: 50%;
  7. top: -0.5em;
  8. background-color: darkred;
  9. left: -9.5em;
  10. z-index: -1;
  11. transform: scale(0.9);
  12. box-shadow: 0 0 1em white;
  13. }

最后,为燃点增加闪动的效果:

  1. .coil::after {
  2. animation: blink 1s linear infinite alternate;
  3. }
  4. @keyframes blink {
  5. to {
  6. box-shadow: 0 0 0 white;
  7. }
  8. }

大功告成!

以上就是如何使用纯CSS实现蚊香燃烧的效果(附源码)的详细内容,更多请关注Gxl网其它相关文章!

人气教程排行