gpt4 book ai didi

reactjs - 如何在使用 React 时将 react-particles-js 设置为背景?

转载 作者:行者123 更新时间:2023-12-02 05:24:29 27 4
gpt4 key购买 nike

我已经设置好了一切,我只是不知道有什么方法可以让 react-particles-js 创建的元素充当背景。

这是我到目前为止的代码:

import React from "react";
import { BrowserRouter as Router, Route } from "react-router-dom";
import NavTabs from "./components/NavTabs";
import Home from "./components/pages/Home";
import About from "./components/pages/About";
import Contact from "./components/pages/Contact";
import ParticlesContainer from "./components/ParticlesContainer";


function App() {
return (
<ParticlesContainer>
<Router>
<div>
<NavTabs />
<Route exact path="/" component={Home} />
<Route exact path="/about" component={About} />
<Route path="/contact" component={Contact} />
</div>
</Router>
</ParticlesContainer>
);
}

export default App;

但是,没有任何内容显示;只有 Canvas 元素可见,而其余元素似乎根本不呈现。

编辑:这是 ParticleContainer 代码:
import React, {Component} from 'react';
import Particles from 'react-particles-js';


class ParticlesContainer extends Component {
render() {
return (
<Particles
params={{
"particles": {
"number": {
"value": 150,
"density": {
"enable": true,
"value_area": 1803.4120608655228
}
},
"color": {
"value": "#ffffff"
},
"shape": {
"type": "circle",
"stroke": {
"width": 2,
"color": "#000000"
},
"polygon": {
"nb_sides": 4
},
"image": {
"src": "img/github.svg",
"width": 100,
"height": 100
}
},
"opacity": {
"value": 0.4008530152163807,
"random": false,
"anim": {
"enable": false,
"speed": 1,
"opacity_min": 0.1,
"sync": false
}
},
"size": {
"value": 1.5,
"random": true,
"anim": {
"enable": false,
"speed": 40,
"size_min": 0.1,
"sync": false
}
},
"line_linked": {
"enable": true,
"distance": 0,
"color": "#ffffff",
"opacity": 0.3687847739990702,
"width": 0.6413648243462091
},
"move": {
"enable": true,
"speed": 6,
"direction": "none",
"random": false,
"straight": false,
"out_mode": "out",
"bounce": false,
"attract": {
"enable": false,
"rotateX": 600,
"rotateY": 1200
}
}
},
"interactivity": {
"detect_on": "window",
"events": {
"onhover": {
"enable": true,
"mode": "repulse"
},
"onclick": {
"enable": false,
"mode": "bubble"
},
"resize": true
},
"modes": {
"grab": {
"distance": 400,
"line_linked": {
"opacity": 1
}
},
"bubble": {
"distance": 400,
"size": 40,
"duration": 2,
"opacity": 8,
"speed": 3
},
"repulse": {
"distance": 100,
"duration": 0.4
},
"push": {
"particles_nb": 4
},
"remove": {
"particles_nb": 2
}
}
},
"retina_detect": true
}} />
)
}
}

export default ParticlesContainer;

最佳答案

包裹您的整个 <Router /><ParticlesContainer />完全不合理,因为您的容器不会渲染任何子项。因此,无形的内容。

我搬家了<ParticlesContainer /><Router /> .在那之后,这只是一个CSS问题。这是一个推荐的工作示例:https://codesandbox.io/s/4k5z9xx0w . (您可以根据自己的喜好调整样式)

作为替代方案,您可以做的是显式渲染子项,但这是不必要的。

export default ({ children }) => (
<>
<Particles />
{children}
</>
);

关于reactjs - 如何在使用 React 时将 react-particles-js 设置为背景?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53604102/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com