vue实现一个离子动画
时间: 2024-05-10 10:20:56 浏览: 64
vue动画效果实现方法示例
Vue 实现离子动画有很多种方式,以下是一种简单的实现方式:
1. 安装 `vue-particles` 库
```bash
npm install vue-particles --save
```
2. 在组件中引入 `vue-particles` 组件
```vue
<template>
<div id="particle-container">
<vue-particles :options="options"></vue-particles>
</div>
</template>
<script>
import VueParticles from 'vue-particles'
export default {
components: {
VueParticles
},
data() {
return {
options: {
particles: {
color: '#fff',
shape: 'circle',
opacity: {
value: 0.8,
random: true,
anim: {
enable: true,
speed: 1,
opacity_min: 0.1,
sync: false
}
},
size: {
value: 5,
random: true,
anim: {
enable: true,
speed: 3,
size_min: 0.1,
sync: false
}
},
line_linked: {
enable: false
},
move: {
enable: true,
speed: 3,
direction: 'none',
random: true,
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: 800,
line_linked: {
opacity: 1
}
},
bubble: {
distance: 800,
size: 80,
duration: 2,
opacity: 0.8,
speed: 3
},
repulse: {
distance: 150,
duration: 0.2
},
push: {
particles_nb: 4
},
remove: {
particles_nb: 2
}
}
},
retina_detect: true
}
}
}
}
</script>
<style>
#particle-container {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
overflow: hidden;
z-index: -1;
}
</style>
```
3. 在 `options` 中设置离子的参数,具体参数参考官方文档:[particles.js](https://github.com/VincentGarreau/particles.js/)
4. 在组件中引入样式
```html
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/vue-particles@1.0.0-rc.3/lib/VueParticles.css">
```
5. 显示离子动画
```html
<particle-animation></particle-animation>
```
完整的组件代码如下:
```vue
<template>
<div id="particle-container">
<vue-particles :options="options"></vue-particles>
</div>
</template>
<script>
import VueParticles from 'vue-particles'
export default {
components: {
VueParticles
},
data() {
return {
options: {
particles: {
color: '#fff',
shape: 'circle',
opacity: {
value: 0.8,
random: true,
anim: {
enable: true,
speed: 1,
opacity_min: 0.1,
sync: false
}
},
size: {
value: 5,
random: true,
anim: {
enable: true,
speed: 3,
size_min: 0.1,
sync: false
}
},
line_linked: {
enable: false
},
move: {
enable: true,
speed: 3,
direction: 'none',
random: true,
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: 800,
line_linked: {
opacity: 1
}
},
bubble: {
distance: 800,
size: 80,
duration: 2,
opacity: 0.8,
speed: 3
},
repulse: {
distance: 150,
duration: 0.2
},
push: {
particles_nb: 4
},
remove: {
particles_nb: 2
}
}
},
retina_detect: true
}
}
}
}
</script>
<style>
#particle-container {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
overflow: hidden;
z-index: -1;
}
</style>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/vue-particles@1.0.0-rc.3/lib/VueParticles.css">
```
阅读全文