要在Vue 3中使用Particles粒子特效,可以使用第三方库particles.js。下面是完整的攻略:
1. 安装particles.js
可以使用npm安装particles.js。
npm install particles.js --save
2. 导入和配置particles.js
在vue的配置文件中(main.js或者App.vue),导入particles.js并进行配置。这里以main.js为例。
import Particles from 'particles.js'
// 设置配置项
const ParticlesConfig = {
particles: {
number: {
value: 80,
density: {
enable: true,
value_area: 800
}
},
color: {
value: '#000000'
},
shape: {
type: 'circle',
stroke: {
width: 0,
color: '#000000'
},
polygon: {
nb_sides: 5
},
},
opacity: {
value: 0.5,
random: false
},
size: {
value: 5,
random: true
},
line_linked: {
enable: true,
distance: 150,
color: '#000000',
opacity: 0.4,
width: 1
},
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: '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
}
Vue.use(Particles, ParticlesConfig)
3. 使用particles.js
之后,可以在需要的页面中加入一个带有id属性的div,并在vue的组件中使用ref找到这个元素。
<div id="particles"></div>
<template>
<div>
<div id="particles" ref="particles">
This is particles.js
</div>
</div>
</template>
<script>
export default {
mounted () {
this.initParticles()
},
methods: {
initParticles () {
// 找到包含particles.js特效的div
let particlesDiv = this.$refs.particles
// 初始化particles.js特效
particlesJS(particlesDiv, {
"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
},
},
"opacity": {
"value": 0.5,
"random": false
},
"size": {
"value": 5,
"random": true
},
"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",
"bounce": false,
"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
})
}
}
}
</script>
4. 示例
下面是两个使用particles.js的实例。
示例1
<template>
<div>
<div id="particles" ref="particles"></div>
<div class="content">
<h1>Hello, particles!</h1>
<p>A lovely day isn't it?</p>
</div>
</div>
</template>
<script>
export default {
mounted () {
this.initParticles()
},
methods: {
initParticles () {
let particlesDiv = this.$refs.particles
particlesJS(particlesDiv, {
"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
},
},
"opacity": {
"value": 0.5,
"random": false
},
"size": {
"value": 5,
"random": true
},
"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",
"bounce": false,
"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
})
}
}
}
</script>
<style>
#particles {
position: absolute;
width: 100%;
height: 100vh;
}
.content {
text-align: center;
margin-top: 200px;
color: #ffffff;
}
</style>
这个例子中,particles.js在整个页面的背景上添加了特效,并在页面内容上方添加了一个白色区块。
示例2
<template>
<div>
<div class="avatar">
<div id="particles" ref="particles"></div>
<img src="./assets/avatar.png" />
</div>
<div class="content">
<h1>Hello, particles!</h1>
<p>A lovely day isn't it?</p>
</div>
</div>
</template>
<script>
export default {
mounted () {
this.initParticles()
},
methods: {
initParticles () {
let particlesDiv = this.$refs.particles
particlesJS(particlesDiv, {
"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
},
},
"opacity": {
"value": 0.5,
"random": false
},
"size": {
"value": 5,
"random": true
},
"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",
"bounce": false,
"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
})
}
}
}
</script>
<style scoped>
.avatar {
text-align: center;
margin-top: 50px;
}
.avatar img {
width: 200px;
border-radius: 50%;
border: 5px solid #ffffff;
}
#particles {
position: absolute;
width: 100%;
height: 100%;
top: 0;
left: 0;
}
.content {
text-align: center;
margin-top: 50px;
color: #ffffff;
}
</style>
这个例子中,particles.js在头像图片背景上添加了特效,并在页面下方添加了一个白色区域。
本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:关于vue3使用particles粒子特效的问题 - Python技术站