【问题标题】:Particles.js background not adjustingParticles.js 背景未调整
【发布时间】:2021-06-07 17:30:46
【问题描述】:

当我调整 Particles.js 的配置时,背景颜色/图像不会改变它的来源。我不确定我做错了什么。这是我的代码:

HTML:

<script src="https://cdn.jsdelivr.net/particles.js/2.0.0/particles.min.js"></script>
<script src="js/app.js"></script>
<script>
    particlesJS.load('particles-js', 'particles.json',
    function(){
        console.log('particles.json loaded...')
    })
</script>
</div>

CSS:

head{
    background-color: gray;
}
body{
    background-color: gray;
}

Particles.json:

{
  "particles": {
    "number": {
      "value": 80,
      "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": 5,
      "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": 6,
      "direction": "none",
      "random": false,
      "straight": false,
      "out_mode": "out",
      "attract": {
        "enable": false,
        "rotateX": 600,
        "rotateY": 1200
      }
    }
  },
  "interactivity": {
    "detect_on": "canvas",
    "events": {
      "onhover": {
        "enable": true,
        "mode": "repulse"
      },
      "onclick": {
        "enable": true,
        "mode": "push"
      },
      "resize": true
    },
    "modes": {
      "grab": {
        "distance": 400,
        "line_linked": {
          "opacity": 1
        }
      },
      "bubble": {
        "distance": 400,
        "size": 40,
        "duration": 2,
        "opacity": 8,
        "speed": 3
      },
      "repulse": {
        "distance": 200
      },
      "push": {
        "particles_nb": 4
      },
      "remove": {
        "particles_nb": 2
      }
    }
  },
  "retina_detect": true,
  "config_demo": {
    "hide_card": false,
    "background_color": "#646570",
    "background_image": "https://images.unsplash.com/photo-1579546929662-711aa81148cf?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxleHBsb3JlLWZlZWR8Mnx8fGVufDB8fHx8&w=1000&q=80",
    "background_position": "50% 50%",
    "background_repeat": "no-repeat",
    "background_size": "cover"
  }
}

我已经尝试了各种背景的十六进制代码,没有一个将它从红色变为红色。我不确定它是从哪里得到红色的,但它是红色的……Background of website

【问题讨论】:

    标签: javascript html particles.js


    【解决方案1】:

    particlesJS("particles-js", {
        "particles": {
            "number": {
                "value": 80,
                "density": {
                    "enable": true,
                    "value_area": 800
                }
            },
            "color": {
                "value": "#ff0000"
            },
            "shape": {
                "type": "circle",
                "stroke": {
                    "width": 0,
                    "color": "#000000"
                },
                "polygon": {
                    "nb_sides": 5
                },
                "image": {
                    "src": "img/github.svg",
                    "width": 100,
                    "height": 100
                }
            },
            "opacity": {
                "value": 1,
                "random": true,
                "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": "#104723",
                "opacity": 0.4,
                "width": 1
            },
            "move": {
                "enable": true,
                "speed": 5,
                "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": true,
                    "mode": "push"
                },
                "resize": true
            },
            "modes": {
                "grab": {
                    "distance": 400,
                    "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
    });
     #particles-js{
         position:absolute;
         width: 100%;
         height: 100%;
         z-index:-1;
         background-color: #ffffff;
         background-image: url("");
         background-repeat: no-repeat;
         background-size: cover;
         background-position: 50% 50%;
    }
    <script src="https://cdn.jsdelivr.net/particles.js/2.0.0/particles.min.js"></script>
    <div id="particles-js"></div>

    我注意到了两件事。您没有在 HTML 中指定 js-particles div。您有一个没有初始 div 标签的结束 div 标签。在您的 &lt;body&gt; 标签之前,创建一个

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

    然后您可以像在我的 sn-p 中一样更改您的 CSS,以及在脚本标签内的粒子函数中使用 JSON,而不是导入 JSON 文件

    【讨论】:

    • 我已经实现了 CSS 修复,并在我的 body 标签之前添加了 div 标签,它仍然显示为红色作为背景。还有什么我可能错过的吗?
    • 我不明白你做错了什么,这个小提琴 (jsfiddle.net/BeerusDev/cmovjk2f/10) 使用了我刚刚放入 HTML 选项卡上的
    • 奇怪...当它在 HTML 上全部输入时,它可以工作,但是当我使用单独的配置时,它不会。嗯,你解决了这个问题。谢谢!
    • 实际上,它只有在我不链接 css 时才有效。如果我链接一个 CSS,无论是否为空,它都会将我的背景变为红色。它忽略了 css 中的任何内容,只是让它变成红色......我很困惑。
    • @JoshSero 使用 !important css 属性
    猜你喜欢
    • 2017-04-04
    • 1970-01-01
    • 1970-01-01
    • 2017-12-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-17
    • 2012-01-14
    相关资源
    最近更新 更多