纯css实现粒子效果

小灰灰 2022-08-27 10:49 662阅读 0赞

前端开发whqet,csdn,王海庆,whqet,前端开发专家

好久没有更新的CSS3演武场系列,半年前搁浅的一篇文章赋予新年新气象闪亮登场了,研究一下利用css(sass)实现粒子效果,主要使用box-shadow生成粒子,利用css3 animation和translateZ实现动画。

-—————————————————————————————-

--我参加了博客之星评选,如果你喜欢我的博客,求投票,您的支持是我的动力之源,走起!

-————————————————————————————————————————————

看效果先

Center

思路解析

1.box-shadow生成粒子,赋予随机位置、随机颜色,利用sass的for循环和random()实现。

2.animation实现动画。

3.transform 3d实现深度动画,主要使用translateZ。

实现

下面我们分别来实现一下,源码我放在了codepen,请大家移步。

-———————-

-———————————————————-

在线研究点击这里,下载收藏点击这里。

-———————————————————-

-———————

1.html部分非常简单就是一个div。

  1. <div class="stars"></div>

2.css部分是重点,我们首先设置全局的css,我们使用normallize和prefix free。然后对html和body作如下设置。

  1. $stars: 100; // 粒子数
  2. $depth: 300; // 深度
  3. $speed: 1s; // 动画时间
  4. $width: 1500; // 宽
  5. $height: 960; // 高
  6. html, body {
  7. height: 100%;
  8. overflow: hidden;
  9. }
  10. body {
  11. perspective: 340px;
  12. background:#000 url(https://img-my.csdn.net/uploads/201501/02/1420174857_3649.png) center center no-repeat;
  13. background-size:28% 30%;
  14. }

对于.stars,我们设置大小、位置,宽、高各位5px,位置居中,圆角边框。

  1. .stars {
  2. position: absolute;
  3. top: 50%;
  4. left: 50%;
  5. width:5px;
  6. height:5px;
  7. border-radius:100%;
  8. }

然后我们使用box-shadow生成粒子。box-shadow一个很好玩的特性,生成效果如下。

  1. .stars {
  2. position: absolute;
  3. top: 50%;
  4. left: 50%;
  5. width:5px;
  6. height:5px;
  7. border-radius:100%;
  8. /*box-shadow生成粒子*/
  9. $box-shadow: ();
  10. @for $i from 0 through $stars {
  11. $box-shadow: $box-shadow, (random($width)-$width/2 + px) (random($height)-$height/2 + px) adjust-hue(#ff0000, random(360));
  12. }
  13. box-shadow: $box-shadow;
  14. }

Center 1

然后这个数量有点少,同时层次感不足,我们利用:before和:after伪对象搞定这些事情。

  1. .stars {
  2. position: absolute;
  3. top: 50%;
  4. left: 50%;
  5. width:5px;
  6. height:5px;
  7. border-radius:100%;
  8. $box-shadow: ();
  9. @for $i from 0 through $stars {
  10. $box-shadow: $box-shadow, (random($width)-$width/2 + px) (random($height)-$height/2 + px) adjust-hue(#ff0000, random(360));
  11. }
  12. box-shadow: $box-shadow;
  13. /*伪对象更多粒子、更多层次*/
  14. &:before, &:after {
  15. content: "";
  16. position: absolute;
  17. width: inherit;
  18. height: inherit;
  19. box-shadow: inherit;
  20. }
  21. &:before {
  22. transform: translateZ(-$depth + px);
  23. opacity: .3;
  24. }
  25. &:after {
  26. transform: translateZ(-$depth * 2 + px);
  27. opacity: .1;
  28. }
  29. }

添加粒子和深度之后效果如下图所示。

Center 2

然后添加动画。

  1. .stars {
  2. position: absolute;
  3. top: 50%;
  4. left: 50%;
  5. width:5px;
  6. height:5px;
  7. border-radius:100%;
  8. /*box-shadow生成粒子*/
  9. $box-shadow: ();
  10. @for $i from 0 through $stars {
  11. $box-shadow: $box-shadow, (random($width)-$width/2 + px) (random($height)-$height/2 + px) adjust-hue(#ff0000, random(360));
  12. }
  13. box-shadow: $box-shadow;
  14. /* 添加动画 */
  15. animation: fly $speed linear infinite;
  16. transform-style:preserve-3d;
  17. /* 伪对象更多粒子、更多层次 */
  18. &:before, &:after {
  19. content: "";
  20. position: absolute;
  21. width: inherit;
  22. height: inherit;
  23. box-shadow: inherit;
  24. }
  25. &:before {
  26. transform: translateZ(-$depth + px);
  27. opacity: .3;
  28. }
  29. &:after {
  30. transform: translateZ(-$depth * 2 + px);
  31. opacity: .1;
  32. }
  33. }
  34. @keyframes fly {
  35. from {
  36. transform: translateZ(0px);
  37. opacity:.1;
  38. }
  39. to {
  40. transform: translateZ($depth + px);
  41. opacity:0.8;
  42. }
  43. }

然后,就可以欣赏效果了。最后给大家附带完整的css源码。

  1. $stars: 100; // 粒子数
  2. $depth: 300; // 深度
  3. $speed: 1s; // 动画时间
  4. $width: 1500; // 宽
  5. $height: 960; // 高
  6. html, body {
  7. height: 100%;
  8. overflow: hidden;
  9. }
  10. body {
  11. perspective: 340px;
  12. background:#000 url(https://img-my.csdn.net/uploads/201501/02/1420174857_3649.png) center center no-repeat;
  13. background-size:28% 30%;
  14. }
  15. .stars {
  16. position: absolute;
  17. top: 50%;
  18. left: 50%;
  19. width:5px;
  20. height:5px;
  21. border-radius:100%;
  22. $box-shadow: ();
  23. @for $i from 0 through $stars {
  24. $box-shadow: $box-shadow, (random($width)-$width/2 + px) (random($height)-$height/2 + px) adjust-hue(#ff0000, random(360));
  25. }
  26. box-shadow: $box-shadow;
  27. /*
  28. animation: fly $speed linear infinite;
  29. transform-style:preserve-3d;
  30. */
  31. &:before, &:after {
  32. content: "";
  33. position: absolute;
  34. width: inherit;
  35. height: inherit;
  36. box-shadow: inherit;
  37. }
  38. &:before {
  39. transform: translateZ(-$depth + px);
  40. opacity: .3;
  41. }
  42. &:after {
  43. transform: translateZ(-$depth * 2 + px);
  44. opacity: .1;
  45. }
  46. }
  47. @keyframes fly {
  48. from {
  49. transform: translateZ(0px);
  50. opacity:.1;
  51. }
  52. to {
  53. transform: translateZ($depth + px);
  54. opacity:0.8;
  55. }
  56. }

感谢您耐心读完,如果对您有帮助,请支持我!

-————————————————————————————-

前端开发whqet,关注web前端开发,分享相关资源,欢迎点赞,欢迎拍砖。
-————————————————————————————————————————————————————

发表评论

表情:
评论列表 (有 0 条评论,662人围观)

还没有评论,来说两句吧...

相关阅读

    相关 css实现粒子效果

    前端开发whqet,csdn,王海庆,whqet,前端开发专家 好久没有更新的CSS3演武场系列,半年前搁浅的一篇文章赋予新年新气象闪亮登场了,研究一下利用css(sass)

    相关 CSS实现手风琴效果

    今天来看一个使用hover触发的一个手风琴效果,鼠标hover时改变图像宽度,配合transition实现动画,效果如下图所示。大家也可以到我的codepen[在线编辑][Li

    相关 css 实现三角效果

    之前写过一篇关于 border 如何实现一个三角形的方法,这一次,我们将在这个的基础上实现下面的效果: ![70][] 关于尺寸方面,自己看着来就行,无所谓的;