资讯专栏INFORMATION COLUMN

前端 CSS : 4# CSS 实现暖暖的小火堆

malakashi / 998人阅读

摘要:介绍原文链接感謝大佬的前端每日实战效果预览浏览源代码地址代码解读首先是完成结构常规样式初始化天上的星星原文中星星是固定位置并且不会闪烁的而这里我们将会改变这一状态而且为了避免重复手动给星星固定位置及大小所以采用了库来减少麻烦首先将改

介绍

</>复制代码

  1. 原文链接

    感謝 comehope 大佬的 [前端每日实战]

效果预览

</>复制代码

  1. github.io 浏览
源代码地址

https://github.com/shanyuhai1...

代码解读 1. 首先是完成 html 结构

</>复制代码

常规样式初始化

</>复制代码

  1. * {
  2. margin: 0;
  3. padding: 0;
  4. }
  5. body {
  6. margin: 0;
  7. padding: 0 0.5vw;
  8. height: 100vh;
  9. background-color: #333;
  10. overflow: hidden;
  11. }
  12. .container {
  13. display: flex;
  14. flex-direction: column;
  15. align-items: center;
  16. justify-content: center;
  17. }
2. 天上的星星

原文中星星是固定位置并且不会闪烁的

而这里我们将会改变这一状态, 而且为了避免重复手动给星星固定位置及大小, 所以采用了 d3 库来减少麻烦

</>复制代码

首先将 .stars 改为 grid 布局

使用 span 标签 作为星星

因为星星要分时间闪烁所以随机一个 --delay 参数

</>复制代码

  1. // index.js
  2. const COLUMNS = 15;
  3. d3.select(".stars")
  4. .style("--columns", COLUMNS)
  5. .selectAll("span")
  6. .data(d3.range(COLUMNS * COLUMNS))
  7. .enter()
  8. .append("span")
  9. .style("--delay", () => Math.random() * 20);

先给出大概的范围, 查看下边界

</>复制代码

  1. .stars {
  2. width: 99vw;
  3. height: 70vh;
  4. position: absolute;
  5. display: grid;
  6. grid-template-columns: repeat(var(--columns), 1fr);
  7. border: 1px solid;
  8. }
  9. .stars span {
  10. width: 0.6vw;
  11. height: 0.6vw;
  12. color: whitesmoke;
  13. background-color: currentColor;
  14. }

星星现在只是一个个正方形, 再给正方形添加上旋转闪现的动画即可

</>复制代码

  1. .stars span {
  2. transform: scale(0);
  3. animation: spin 20s linear infinite;
  4. animation-delay: calc(var(--delay) * 1s);
  5. }
  6. @keyframes spin {
  7. 0% {
  8. transform: rotate(0deg) scale(1);
  9. }
  10. 5%,
  11. 15% {
  12. transform: rotate(90deg) scale(0);
  13. background: goldenrod;
  14. }
  15. 17.5% {
  16. transform: rotate(180deg) scale(1);
  17. background-color: currentColor;
  18. }
  19. 20%,
  20. 100% {
  21. transform: rotate(90deg) scale(0);
  22. }
  23. }
3. 添加火堆

首先是修改 DOM

</>复制代码

使火堆居中, 利用媒体查询改变一下在手机端偏小的问题

</>复制代码

  1. .fires {
  2. position: relative;
  3. border: 1px solid;
  4. }
  5. @media screen and (min-width: 451px) {
  6. .fires {
  7. width: 15vw;
  8. height: 15vw;
  9. margin-top: -7vw;
  10. }
  11. }
  12. @media screen and (max-width: 450px) {
  13. .fires {
  14. width: 18vh;
  15. height: 18vh;
  16. margin-top: -5vw;
  17. }
  18. }

接着完成火焰效果, 在父级添加可用的 color , border-radius 变量

</>复制代码

  1. .fires {
  2. position: relative;
  3. display: flex;
  4. align-items: center;
  5. justify-content: center;
  6. border: 1px solid;
  7. --color-one: #D92B29;
  8. --color-two: #F5732A;
  9. --color-three: #F2B338;
  10. --color-four: #F5D549;
  11. --shape-one: 79% 21% 64% 36% / 43% 61% 39% 57%;
  12. --shape-two: 23% 77% 66% 34% / 57% 72% 28% 43%;
  13. --shape-three: 78% 22% 63% 37% / 39% 27% 73% 61%;
  14. --shape-four: 35% 65% 78% 22% / 54% 50% 50% 46%;
  15. }
  16. @media screen and (min-width: 451px) {
  17. .fires__flame {
  18. width: 6vw;
  19. }
  20. .fires__flame:nth-of-type(1) {
  21. height: 15vw;
  22. }
  23. .fires__flame:nth-of-type(2) {
  24. height: 12vw;
  25. transform: translate(2.25vw, 1.2vw) rotate(30deg);
  26. }
  27. .fires__flame:nth-of-type(3) {
  28. height: 13.5vw;
  29. transform: translate(-2.25vw, 1.2vw) rotate(-30deg);
  30. }
  31. .fires__flame:nth-of-type(4) {
  32. height: 10.5vw;
  33. }
  34. }
  35. @media screen and (max-width: 450px) {
  36. .fires__flame {
  37. width: 7.2vh;
  38. }
  39. .fires__flame:nth-of-type(1) {
  40. height: 18vh;
  41. }
  42. .fires__flame:nth-of-type(2) {
  43. height: 14.4vh;
  44. transform: translate(2.7vh, 1.44vh) rotate(30deg);
  45. }
  46. .fires__flame:nth-of-type(3) {
  47. height: 16.2vh;
  48. transform: translate(-2.7vh, 1.44vh) rotate(-30deg);
  49. }
  50. .fires__flame:nth-of-type(4) {
  51. height: 12.6vh;
  52. }
  53. }
  54. .fires__flame {
  55. position: absolute;
  56. background-color: var(--color-one);
  57. border-radius: var(--shape-one);
  58. z-index: 0;
  59. animation-name: fire;
  60. animation-duration: 1.5s;
  61. animation-iteration-count: infinite;
  62. transition: ease 0.4s;
  63. }
  64. .fires__flame:nth-of-type(2) {
  65. border-radius: var(--shape-two);
  66. background-color: var(--color-two);
  67. opacity: 0.9;
  68. z-index: 2;
  69. animation-delay: 0.2s;
  70. }
  71. .fires__flame:nth-of-type(3) {
  72. border-radius: var(--shape-three);
  73. background-color: var(--color-three);
  74. opacity: 0.8;
  75. z-index: 1;
  76. animation-delay: 0.4s;
  77. }
  78. .fires__flame:nth-of-type(4) {
  79. border-radius: var(--shape-four);
  80. background-color: var(--color-four);
  81. opacity: 0.8;
  82. z-index: 1;
  83. animation-delay: 0.6s;
  84. }

当然别忘了火焰的动画效果

</>复制代码

  1. @keyframes fire {
  2. 0% {
  3. border-radius: var(--shape-one);
  4. background-color: var(--color-one);
  5. }
  6. 25% {
  7. border-radius: var(--shape-two);
  8. background-color: var(--color-two);
  9. }
  10. 50% {
  11. border-radius: var(--shape-three);
  12. background-color: var(--color-three);
  13. }
  14. 75% {
  15. border-radius: var(--shape-four);
  16. background-color: var(--color-four);
  17. }
  18. 100% {
  19. border-radius: var(--shape-one);
  20. background-color: var(--color-one);
  21. }
  22. }

再添加木柴

</>复制代码

  1. @media screen and (min-width: 451px) {
  2. .fires__stick {
  3. border-radius: 1.5vw;
  4. width: 3vw;
  5. height: 13.5vw;
  6. bottom: -7.5vw;
  7. }
  8. }
  9. @media screen and (max-width: 450px) {
  10. .fires__stick {
  11. border-radius: 1.8vh;
  12. width: 3.6vh;
  13. height: 16.2vh;
  14. bottom: -9vh;
  15. }
  16. }
  17. .fires__stick {
  18. background-color: #5a3600;
  19. position: absolute;
  20. z-index: 2;
  21. transform:rotate(-70deg);
  22. }
  23. .fires__stick:last-of-type {
  24. transform:rotate(70deg);
  25. background-color: #4e2f01;
  26. }
4. 最后

最后记得把之前确认位置及大小的 border 边框删除即可

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/117129.html

相关文章

  • 利用CSS改变图片颜色的100种方法!

    摘要:前言说到对图片进行处理,我们经常会想到这类的图像处理工具。或者是的时候,对图片的对比度,阴影进行处理。过滤器通常被用于调整图片,背景和边界的渲染。最后,安利我们的公众号前端指南。 前言 说到对图片进行处理,我们经常会想到PhotoShop这类的图像处理工具。作为前端开发者,我们经常会需要处理一些特效,例如根据不同的状态,让图标显示不同的颜色。或者是hover的时候,对图片的对比度,阴影...

    Keven 评论0 收藏0
  • 利用CSS改变图片颜色的100种方法!

    摘要:前言说到对图片进行处理,我们经常会想到这类的图像处理工具。或者是的时候,对图片的对比度,阴影进行处理。过滤器通常被用于调整图片,背景和边界的渲染。最后,安利我们的公众号前端指南。 前言 说到对图片进行处理,我们经常会想到PhotoShop这类的图像处理工具。作为前端开发者,我们经常会需要处理一些特效,例如根据不同的状态,让图标显示不同的颜色。或者是hover的时候,对图片的对比度,阴影...

    pinecone 评论0 收藏0
  • 用Node EJS写一个爬虫脚本每天定时给心爱的她发一封暖心邮件

    摘要:本文首发于个人博客项目源码,欢迎,说不定哪天脱单了就能用到了写在前面自从用邮箱注册了很多账号后,便会收到诸如以下类似的邮件刚开始还以为是一张图片,后来仔细一看不是图片呀,好像还是呀,于是好奇宝宝我一下,查阅多篇资料后总结出怎么用前端知识和做 本文首发于个人博客:VinceBlog 项目源码:NodeMail,欢迎star,说不定哪天脱单了就能用到了 写在前面 自从用邮箱注册了很多账号后...

    zero 评论0 收藏0
  • 2017文章总结

    摘要:欢迎来我的个人站点性能优化其他优化浏览器关键渲染路径开启性能优化之旅高性能滚动及页面渲染优化理论写法对压缩率的影响唯快不破应用的个优化步骤进阶鹅厂大神用直出实现网页瞬开缓存网页性能管理详解写给后端程序员的缓存原理介绍年底补课缓存机制优化动 欢迎来我的个人站点 性能优化 其他 优化浏览器关键渲染路径 - 开启性能优化之旅 高性能滚动 scroll 及页面渲染优化 理论 | HTML写法...

    dailybird 评论0 收藏0

发表评论

0条评论

malakashi

|高级讲师

TA的文章

阅读更多
最新活动
阅读需要支付1元查看
<