如何在React中使粒子效果在横幅部分结束后消失?

huangapple go评论101阅读模式
英文:

How to make particles js to disappear after banner section end in react

问题

  1. <canvas data-generated="false" style="width: 100% !important; height: 100% !important; position: fixed !important; z-index: -1 !important; top: 0px !important; left: 0px !important; background-color: rgb(0, 0, 0); pointer-events: none;" aria-hidden="true" width="133" height="640"></canvas>
  1. export default function Banner() {
  2. return (
  3. <section className="banner">
  4. <ParticlesBg className="bg" />
  5. <main>
  6. <h1>你好世界</h1>
  7. </main>
  8. </section>
  9. )
  10. }
  1. export function ParticlesBg() {
  2. const particlesInit = useCallback(async engine => {
  3. console.log(engine);
  4. // you can initiate the tsParticles instance (engine) here, adding custom shapes or presets
  5. // this loads the tsparticles package bundle, it's the easiest method for getting everything ready
  6. // starting from v2 you can add only the features you need reducing the bundle size
  7. await loadFull(engine);
  8. }, []);
  9. const particlesLoaded = useCallback(async container => {
  10. await console.log(container);
  11. }, []);
  12. return (
  13. <Particles
  14. id="tsparticles"
  15. init={particlesInit}
  16. loaded={particlesLoaded}
  17. options={{
  18. fullScreen: {
  19. "enable": true,
  20. "zIndex": -1,
  21. },
  22. background: {
  23. color: {
  24. value: "#000000",
  25. },
  26. },
  27. // ... (其余未翻译内容)
  28. }}
  29. />
  30. );
  31. }
  1. .banner{
  2. width: 100%;
  3. height: 100vh;
  4. }
  5. .banner h1 {
  6. font-size: 60px;
  7. color: white;
  8. }
  9. #particles-canvas {
  10. position: absolute !important;
  11. }
英文:

I built a banner section with particles js background. and I want start new section but the particles js background is fixed position important. I want to make the particles disappear after the banner end .
I opend the developer tools and this is the code of canvas:

  1. <canvas data-generated="false" style="width: 100% !important; height: 100% !important; position: fixed !important; z-index: -1 !important; top: 0px !important; left: 0px !important; background-color: rgb(0, 0, 0); pointer-events: none;" aria-hidden="true" width="133" height="640"></canvas>

Banner.js code:

  1. import { ParticlesBg } from "./ParticlesBg"
  2. import './banner.css'
  3. export default function Banner() {
  4. return (
  5. <section className="banner">
  6. <ParticlesBg className="bg" />
  7. <main>
  8. <h1>Hello World</h1>
  9. </main>
  10. </section>
  11. )
  12. }

ParticlesBg.js code:

  1. import { useCallback } from "react";
  2. import Particles from "react-tsparticles";
  3. import { loadFull } from "tsparticles";
  4. export function ParticlesBg() {
  5. const particlesInit = useCallback(async engine => {
  6. console.log(engine);
  7. // you can initiate the tsParticles instance (engine) here, adding custom shapes or presets
  8. // this loads the tsparticles package bundle, it's the easiest method for getting everything ready
  9. // starting from v2 you can add only the features you need reducing the bundle size
  10. await loadFull(engine);
  11. }, []);
  12. const particlesLoaded = useCallback(async container => {
  13. await console.log(container);
  14. }, []);
  15. return (
  16. <Particles
  17. id="tsparticles"
  18. init={particlesInit}
  19. loaded={particlesLoaded}
  20. options={{
  21. fullScreen: {
  22. "enable": true,
  23. "zIndex": -1,
  24. },
  25. background: {
  26. color: {
  27. value: "#000000",
  28. },
  29. },
  30. fpsLimit: 60,
  31. interactivity: {
  32. events: {
  33. onClick: {
  34. enable: false,
  35. mode: "push",
  36. },
  37. onHover: {
  38. enable: false,
  39. mode: "repulse",
  40. },
  41. resize: true,
  42. },
  43. modes: {
  44. push: {
  45. quantity: 4,
  46. },
  47. repulse: {
  48. distance: 200,
  49. duration: 0.4,
  50. },
  51. },
  52. position: "absolute"
  53. },
  54. particles: {
  55. color: {
  56. value: "#ffffff",
  57. },
  58. links: {
  59. color: "#ffffff",
  60. distance: 150,
  61. enable: true,
  62. opacity: 0.5,
  63. width: 1,
  64. },
  65. collisions: {
  66. enable: true,
  67. },
  68. move: {
  69. directions: "none",
  70. enable: true,
  71. outModes: {
  72. default: "bounce",
  73. },
  74. random: false,
  75. speed: 1,
  76. straight: false,
  77. },
  78. number: {
  79. density: {
  80. enable: true,
  81. area: 800,
  82. },
  83. value: 80,
  84. },
  85. opacity: {
  86. value: 0.5,
  87. },
  88. shape: {
  89. type: "circle",
  90. },
  91. size: {
  92. value: { min: 1, max: 5 },
  93. },
  94. },
  95. detectRetina: true,
  96. }}
  97. />
  98. );
  99. }

CSS code:

  1. .banner{
  2. width: 100%;
  3. height: 100vh;
  4. }
  5. .banner h1 {
  6. font-size: 60px;
  7. color: white;
  8. }
  9. #particles-canvas {
  10. position: absolute !important;
  11. }

答案1

得分: 0

在CSS代码中:

  1. section:not(.banner) {
  2. background-color: white;
  3. }
英文:

In CSS code:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-css -->

  1. section:not(.banner) {
  2. background-color: white;
  3. }

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年2月19日 00:42:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/75494808.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定