particles.js在vue上的運用

轉:https://www.jianshu.com/p/c52b3e91c94fjavascript

知乎的首頁後面的粒子動效總以爲很炫酷,搜了一下,發現是用particles.js編寫的。恰好目前的項目是利用vue框架的,兩個湊在一塊兒學了。
講道理,這個用得好的話,頁面是能夠很酷的,譬如我如今寫的項目css

 
酷酷的登陸頁

嘻嘻~vue

安裝particles.js


npm install --save particles.js 

配置particles.js


  1. template
    這個就是動態粒子要展現的位置。
<div id="particles"></div> 
  1. script
    由於涉及到dom樹,因此必須在掛載結束後初始化particles.js。第一個參數id就是你在template上取得id名,像我要寫的話就是particles。第二個參數是你的data存放的路徑,我的建議使用相對路徑。
mounted(){ particlesJS.load('id','path to your particles.data'); } 
  1. style
#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) 
  1. particles.data
    這個data是用於控制粒子在頁面中所呈現的狀態,這個文件是本身新建的,建議放在靜態資源文件夾裏。經過修改裏邊的字段,來獲得本身想要的效果。如修改particles.color.value 的值就是修改粒子的顏色;修改particle.shape就是修改粒子的外觀。
{
  "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

相關文章
相關標籤/搜索