如何在页面中多次使用particles.js?

时间:2015-09-05 15:38:15

标签: javascript jquery html css

我想知道如何在一个页面中多次添加更多粒子.js?看来你每页只能做一次。但我想不止一次地使用它,就像在两个独立的div中一样。我怎样才能做到这一点? 我只能做一次,但似乎无法在另一个div中再做一次。

以下是该插件的链接 - https://github.com/VincentGarreau/particles.js/

感谢您的时间。

var canvas_el = document.querySelector('#'+tag_id+' > .particles-js-canvas-el');

2 个答案:

答案 0 :(得分:4)

看看这个:http://codepen.io/anon/pen/avzPGm

<div id="particles-js"></div>
<div id="particles-js2"></div>

然后:

particlesJS("particles-j2", {...});
particlesJS("particles-js2", {...});

您可以调用particleJS两次,每个元素调用一次。

答案 1 :(得分:0)

看看这个:https://codepen.io/ngocquydhtn/pen/VwePzmL

我的代码:

HTML:

<div id="particle-container" class="particle-class"></div>
<div id="particle-container2" class="particle-class"></div>
<div id="particle-container3" class="particle-class"></div>
<div id="particle-container4" class="particle-class"></div>
<div id="particle-container5" class="particle-class"></div>

JS:

jQuery(document).ready(function($) {
  $(".particle-class").each(function() {
    particlesJS($(this).attr('id'), {
    "particles": {
      "number": {
        "value": 80,
        "density": {
          "enable": true,
          "value_area": 800
        }
      },
      "color": {
        "value": "random"
      },
      "shape": {
        "type": "circle",
        "stroke": {
          "width": 0,
          "color": "random"
        },
        "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": 10,
        "random": true,
        "anim": {
          "enable": false,
          "speed": 40,
          "size_min": 2,
          "sync": false
        }
      },
      "line_linked": {
        "enable": false,
        "distance": 150,
        "color": "#ff6d34",
        "opacity": 0.4,
        "width": 1
      },
      "move": {
        "enable": true,
        "speed": 2,
        "direction": "none",
        "random": true,
        "straight": false,
        "out_mode": "out",
        "bounce": false,
        "attract": {
          "enable": false,
          "rotateX": 600,
          "rotateY": 1200
        }
      }
    },
    "interactivity": {
      "detect_on": "canvas",
      "events": {
        "onhover": {
          "enable": true,
          "mode": "grab"
        },
        "onclick": {
          "enable": false,
          "mode": "push"
        },
        "resize": true
      },
      "modes": {
        "grab": {
          "distance": 150,
          "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
  });

});
});