HTML脚本
<div id="particles-js"></div>
<script src="https://cdn.jsdelivr.net/particles.js/2.0.0/particles.min.js"></script>
<script>
particlesJS.load('particles-js', 'particles.json',
function(){
console.log('particles.json loaded...')
})
</script>
粒子的CSS
#particles-js {
margin-top: 100px;
width: 100vw;
height: 400px;
background: linear-gradient(45deg, #0ac8f8, #081866);
z-index: 9;
}
现在每当我加载页面时,我都会收到这些错误(在控制台中):
particles.min.js:9 Failed to load file:///G:/actualtest/particles.json: Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, https.
window.particlesJS.load @ particles.min.js:9
(anonymous) @ home.html:66
particles.min.js:9 Error pJS - XMLHttpRequest status: 0
particles.min.js:9 Error pJS - File config not found
答案 0 :(得分:1)
正如@TemaniAfif所指出的,这里有一个缺少的逗号:
<script>
particlesJS.load('particles-js', 'particles.json'
function(){
console.log('particles.json loaded...')'
})
</body>
...因为底部的内联函数是supposed,是particleJS.load函数调用的参数。它需要一个结束脚本标记。尝试将以上内容替换为:
<script>
particlesJS.load('particles-js', 'particles.json',
function(){
console.log('particles.json loaded...')'
})
</script>
</body>
答案 1 :(得分:1)
请尝试以下代码:
<script>
particlesJS.load('particles-js', 'particles.json',
function(){
console.log("particles.json loaded...");
});
</script>
您是否在运行应用程序的根目录中正确保存了“particle.json”文件?