谁能解释如何为 Angular 6 项目添加粒子 js 背景?我按照以下链接进行了一些教程。但它对我不起作用。 https://github.com/VincentGarreau/particles.js/
谢谢你。
谁能解释如何为 Angular 6 项目添加粒子 js 背景?我按照以下链接进行了一些教程。但它对我不起作用。 https://github.com/VincentGarreau/particles.js/
谢谢你。
这就是我让它在我的 NG6 项目中工作的方式:
在您的组件 ngOnInit 中添加以下代码:
particlesJS.load('particles-js', 'assets/data/particles.json', function() { console.log('callback -particles.js 配置加载'); });
希望这可以帮助!
编辑:添加代码
import { Component, OnInit } from '@angular/core';
import { ParticlesConfig } from './../../../../../assets/data/particles';
declare var particlesJS: any;
@Component({
selector: 'app-heading',
templateUrl: './heading.component.html',
styleUrls: ['./heading.component.scss']
})
export class HeadingComponent implements OnInit {
constructor() { }
ngOnInit() {
// https://vincentgarreau.com/particles.js/
particlesJS('particles-js', ParticlesConfig, function() {
console.log('callback - particles.js config loaded');
});
}
}
模板
<div class="h-75 bg header">
<div id="particles-js" class="w-100 header-particles" >
</div>
<div class="header-container w-100">
<div>
<h1> Big Header</h1>
<div>small header</div>
</div>
</div>
</div>
以及在另一个组件中的使用
<app-heading></app-heading>
<main>
<app-features></app-features>
<app-pricing-tier></app-pricing-tier>
<app-testimonials></app-testimonials>
<app-trusted-by></app-trusted-by>
</main>
我想在Alberto的回答中添加更多内容。我在 Angular CLI 8.3.2 版上,一切正常。正如问题所问,我实际上想将它添加到我的组件的背景中。我使用这样的 CSS 实现了这一点。
HTML
<div id="container">
<div id="particles-js">
</div>
<div id="over">
<!--Existing markup-->
</div>
</div>
CSS
#container {
width: 100px;
height: 100px;
position: relative;
}
#particles-js,
#over {
width: 100%;
height: 100%;
position: absolute;
top: 0;
left: 0;
}
#over {
z-index: 10;
}
此设置将在您现有的标记下应用particles.js 背景。
编辑:
如果您使用 Windows 上的 Azure 应用服务 (IIS) 将其部署到生产环境,您可能会收到 404 not found 错误particles.json
。在这种情况下,在文件夹中创建web.config
如下src
文件,并将其包含在assets
数组中angular.json
网络配置
<configuration>
<system.webServer>
<staticContent>
<mimeMap fileExtension=".json" mimeType="application/json" />
</staticContent>
<rewrite>
<rules>
<rule name="Angular" stopProcessing="true">
<match url=".*" />
<conditions logicalGrouping="MatchAll">
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
</conditions>
<action type="Rewrite" url="/" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>
角.json
"assets": [
"projects/dashboard/src/favicon.ico",
"projects/dashboard/src/assets",
"projects/dashboard/src/web.config"
]
您可以使用“angular-particle”轻松实现粒子动画,它是带有 TypeScript for Angular 的particle.js 实现
它的实现非常简单,您可以在下面的链接中找到它 https://www.npmjs.com/package/angular-particle
编辑:
这是 angular 8 中 angular-particle 的运行示例 https://github.com/SunnyMakode/angular-particle-demo
从 github 拉取代码后,