How to set React-Particles-Js to background using React?

巧了我就是萌 提交于 2020-01-03 05:26:09

问题


I am learning React and having some issues setting React-Particles-Js (https://www.npmjs.com/package/react-particles-js) to be the background of my website.

If I only render

  class App extends Component {
  render() {
    return (
      <div>
      <Particles />
      </div>

    );
  }
}

I get the background just as I wish it to be. However, as soon as I render anything else (for example an h1 tag), it is not displayed on react-particles-js, so to say, but instead it moves react-particles-js and is displayed separately.

For example, if I render

 class App extends Component {
      render() {
        return (
          <div>
          <h1>Hello World</h1>
          <h1>Hello World</h1>
          <h1>Hello World</h1>
          <Particles />


          </div>


        );
      }
    }

    export default App;

what happens is that "Hello World" is displayed three times in the top left hand corner of the screen and Particles is displayed below it, meaning that Particles is interpreted as just another element on the same layer as h1 (as if it was another h1 tag) rather than as a background element that underlies all elements--whether they are h1 tags, cards, or navs or anything else--which is what I'd like it to be!

This is my Particles.js

import React, { Component } from 'react';
import Particles from 'react-particles-js';

var style = {
    width: "100vw",
    height: "100vh",
};


class ParticlesBackground extends Component {

    render() {
        return (
            <div style={style}>
            <Particles
                params={{
                    "particles": {
                    "number": {
                    "value": 90
                    },
                    "size": {
                    "value": 2.5
                    }
                },
                    "interactivity": {
                    "events": {
                    "onhover": {
                    "enable": true,
                    "mode": "repulse"
                    }
                    }
                    }
                }}/>
            </div>
            )
    }
}

export default ParticlesBackground;

This is my App.js

import React, { Component } from 'react';
import logo from './logo.svg';
import './App.css';
import Nav from './Nav'
import MainArea from './MainArea';
import Particles from './Particles';
import PeopleCard from './PeopleCard'





class App extends Component {
  render() {
    return (
      <div>
      <h1>Hello World</h1>
      <h1>Hello World</h1>
      <h1>Hello World</h1>
      <Particles />


      </div>


    );
  }
}

export default App;

And this is what I see enter image description here As you can see, the particles and the tags seem to be interpreted as being mutually exclusive.

(PS, I set the body tag in my html.index to have the background color #e74c3c, which is the red-ish you see as the background)

Any advice on how I could fix this?


回答1:


I use the same lib for particles, here is my canvas css that works just like you want:

#particle-canvas {
    position:fixed !important;
    left:0;
    top:0;
    width:100%;
    height:100%;
}

It fixes element position and set it to top-left most of the screen, setting to 100% on both dimenions.



来源:https://stackoverflow.com/questions/55101135/how-to-set-react-particles-js-to-background-using-react

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!