純css實現粒子效果,css實現粒子

來源:互聯網
上載者:User

純css實現粒子效果,css實現粒子
前端開發whqet,csdn,王海慶,whqet,前端開發專家

好久沒有更新的CSS3演武場系列,半年前擱淺的一篇文章賦予新年新氣象閃亮登場了,研究一下利用css(sass)實現粒子效果,主要使用box-shadow產生粒子,利用css3 animation和translateZ實現動畫。

------------------------------------------------------------

--我參加了部落格之星評選,如果你喜歡我的部落格,求投票,您的支援是我的動力之源,走起!

-----------------------------------------------------------------------------------------

看效果先


思路解析

1.box-shadow產生粒子,賦予隨機位置、隨機顏色,利用sass的for迴圈和random()實現。

2.animation實現動畫。

3.transform 3d實現深度動畫,主要使用translateZ。

實現

下面我們分別來實現一下,源碼我放在了codepen,請大家移步。

----------------

----------------------------------------

線上研究點擊這裡,下載收藏點擊這裡。

----------------------------------------

---------------

1.html部分非常簡單就是一個div。

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

2.css部分是重點,我們首先設定全域的css,我們使用normallize和prefix free。然後對html和body作如下設定。

$stars: 100;         // 粒子數$depth: 300;         // 深度$speed: 1s;          // 動畫時間$width: 1500;        // 寬$height: 960;        // 高html, body {  height: 100%;  overflow: hidden;}body {  perspective: 340px;  background:#000 url(http://img.my.csdn.net/uploads/201501/02/1420174857_3649.png) center center no-repeat;  background-size:28% 30%;}

對於.stars,我們設定大小、位置,寬、高各位5px,位置置中,圓角邊框。

.stars {  position: absolute;  top: 50%;  left: 50%;  width:5px;  height:5px;  border-radius:100%;}

然後我們使用box-shadow產生粒子。box-shadow一個很好玩的特性,產生效果如下。

.stars {  position: absolute;  top: 50%;  left: 50%;  width:5px;  height:5px;  border-radius:100%;  /*box-shadow產生粒子*/  $box-shadow: ();  @for $i from 0 through $stars {    $box-shadow: $box-shadow, (random($width)-$width/2 + px) (random($height)-$height/2 + px) adjust-hue(#ff0000, random(360));  }  box-shadow: $box-shadow;}


然後這個數量有點少,同時層次感不足,我們利用:before和:after偽對象搞定這些事情。

.stars {  position: absolute;  top: 50%;  left: 50%;  width:5px;  height:5px;  border-radius:100%;  $box-shadow: ();  @for $i from 0 through $stars {    $box-shadow: $box-shadow, (random($width)-$width/2 + px) (random($height)-$height/2 + px) adjust-hue(#ff0000, random(360));  }  box-shadow: $box-shadow;  /*偽對象更多粒子、更多層次*/  &:before, &:after {    content: "";    position: absolute;    width: inherit;    height: inherit;    box-shadow: inherit;  }  &:before {    transform: translateZ(-$depth + px);    opacity: .3;  }  &:after {    transform: translateZ(-$depth * 2 + px);    opacity: .1;  }}

添加粒子和深度之後效果如所示。


然後添加動畫。

.stars {  position: absolute;  top: 50%;  left: 50%;  width:5px;  height:5px;  border-radius:100%;  /*box-shadow產生粒子*/  $box-shadow: ();  @for $i from 0 through $stars {    $box-shadow: $box-shadow, (random($width)-$width/2 + px) (random($height)-$height/2 + px) adjust-hue(#ff0000, random(360));  }  box-shadow: $box-shadow;  /* 添加動畫 */  animation: fly $speed linear infinite;  transform-style:preserve-3d;  /* 偽對象更多粒子、更多層次 */  &:before, &:after {    content: "";    position: absolute;    width: inherit;    height: inherit;    box-shadow: inherit;  }  &:before {    transform: translateZ(-$depth + px);    opacity: .3;  }  &:after {    transform: translateZ(-$depth * 2 + px);    opacity: .1;  }}@keyframes fly {  from {    transform: translateZ(0px);    opacity:.1;  }  to {    transform: translateZ($depth + px);    opacity:0.8;  }}

然後,就可以欣賞效果了。最後給大家附帶完整的css源碼。

$stars: 100;         // 粒子數$depth: 300;         // 深度$speed: 1s;          // 動畫時間$width: 1500;        // 寬$height: 960;        // 高html, body {  height: 100%;  overflow: hidden;}body {  perspective: 340px;  background:#000 url(http://img.my.csdn.net/uploads/201501/02/1420174857_3649.png) center center no-repeat;  background-size:28% 30%;}.stars {  position: absolute;  top: 50%;  left: 50%;  width:5px;  height:5px;  border-radius:100%;  $box-shadow: ();  @for $i from 0 through $stars {    $box-shadow: $box-shadow, (random($width)-$width/2 + px) (random($height)-$height/2 + px) adjust-hue(#ff0000, random(360));  }  box-shadow: $box-shadow; /*   animation: fly $speed linear infinite;  transform-style:preserve-3d;  */  &:before, &:after {    content: "";    position: absolute;    width: inherit;    height: inherit;    box-shadow: inherit;  }  &:before {    transform: translateZ(-$depth + px);    opacity: .3;  }  &:after {    transform: translateZ(-$depth * 2 + px);    opacity: .1;  }}@keyframes fly {  from {    transform: translateZ(0px);    opacity:.1;  }  to {    transform: translateZ($depth + px);    opacity:0.8;  }}

感謝您耐心讀完,如果對您有協助,請支援我!

----------------------------------------------------------

前端開發whqet,關注web前端開發,分享相關資源,歡迎點贊,歡迎拍磚。
---------------------------------------------------------------------------------------------------------

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.