轉:https://www.jianshu.com/p/c52b3e91c94fjavascript
知乎的首頁後面的粒子動效總以爲很炫酷,搜了一下,發現是用particles.js編寫的。恰好目前的項目是利用vue框架的,兩個湊在一塊兒學了。
講道理,這個用得好的話,頁面是能夠很酷的,譬如我如今寫的項目css
嘻嘻~vue
npm install --save particles.js
<div id="particles"></div>
mounted(){ particlesJS.load('id','path to your particles.data'); }
#particles{ position: absolute; width: 100%; height: 100%; background-color: #b61924; background-repeat: no-repeat; background-size: cover; background-position: 50% 50%; }
講到這裏會發現,還有一個最重要的點沒講出來,恩,就是particles.js 的引入。當你的使用範圍比較小時,能夠直接在當前vue文件的script中引入,即java
//vue文件 import particles from 'particles.js'
又或者你以爲這樣很差管理,必定要放在main文件中也能夠git
//main文件 import particles from 'particles.js' Vue.use(particles)
{
"particles": { "number": { "value": 60, "density": { "enable": true, "value_area": 800 } }, "color": { "value": "#ffffff" }, "shape": { "type": "circle", "stroke": { "width": 0, "color": "#000000" }, "polygon": { "nb_sides": 5 }, "image": { "src": "img/github.svg", "width": 100, "height": 100 } }, "opacity": { "value": 0.5, "random": false, "anim": { "enable": false, "speed": 1, "opacity_min": 0.1, "sync": false } }, "size": { "value": 3, "random": true, "anim": { "enable": false, "speed": 40, "size_min": 0.1, "sync": false } }, "line_linked": { "enable": true, "distance": 150, "color": "#ffffff", "opacity": 0.4, "width": 1 }, "move": { "enable": true, "speed": 4, "direction": "none", "random": false, "straight": false, "out_mode": "out", "bounce": false, "attract": { "enable": false, "rotateX": 100, "rotateY": 1200 } } }, "interactivity": { "detect_on": "Window", "events": { "onhover": { "enable": true, "mode": "grab" }, "onclick": { "enable": true, "mode": "push" }, "resize": true }, "modes": { "grab": { "distance": 140, "line_linked": { "opacity": 1 } }, "bubble": { "distance": 400, "size": 40, "duration": 2, "opacity": 8, "speed": 3 }, "repulse": { "distance": 200, "duration": 0.4 }, "push": { "particles_nb": 4 }, "remove": { "particles_nb": 2 } } }, "retina_detect": true }
最後呈現的效果以下github
嘻嘻,記錄完了,能夠把test文件刪了。npm