vue实现水波球进度条
Vue 实现水波球进度条
使用 SVG 和动态计算
在 Vue 中实现水波球进度条,可以通过 SVG 结合动态计算波浪效果。以下是一个基于 Vue 3 的实现方法:
<template>
<div class="wave-ball">
<svg viewBox="0 0 100 100" class="wave-svg">
<defs>
<clipPath id="wave-clip">
<path :d="wavePath" fill="none" stroke="none" />
</clipPath>
</defs>
<circle cx="50" cy="50" r="45" fill="#e0e0e0" />
<circle cx="50" cy="50" r="45" fill="#4a90e2" clip-path="url(#wave-clip)" />
</svg>
<div class="progress-text">{{ progress }}%</div>
</div>
</template>
<script>
import { ref, computed, onMounted } from 'vue'
export default {
setup() {
const progress = ref(75)
const animationOffset = ref(0)
const wavePath = computed(() => {
const amplitude = 5
const frequency = 0.02
const height = 100 - progress.value
let path = `M 0 ${height}`
for (let x = 0; x <= 100; x++) {
const y = amplitude * Math.sin(x * frequency + animationOffset.value) + height
path += ` L ${x} ${y}`
}
path += ` L 100 100 L 0 100 Z`
return path
})
onMounted(() => {
setInterval(() => {
animationOffset.value += 0.1
}, 100)
})
return { progress, wavePath }
}
}
</script>
<style>
.wave-ball {
position: relative;
width: 200px;
height: 200px;
}
.wave-svg {
width: 100%;
height: 100%;
}
.progress-text {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
font-size: 24px;
font-weight: bold;
color: #333;
}
</style>
使用第三方库
对于更复杂的效果或简化开发,可以使用专门的水波球组件库:
-
安装
vue-wave-progress库:
npm install vue-wave-progress -
在组件中使用:
<template> <wave-progress :progress="progress" :options="waveOptions" /> </template>
export default { components: { WaveProgress }, data() { return { progress: 65, waveOptions: { waveHeight: 10, waveAnimation: true, color: '#3a7bd5', textColor: '#333' } } } }

CSS 动画增强效果
可以通过 CSS 动画增强水波动画效果:
.wave-animation {
animation: wave 3s infinite linear;
}
@keyframes wave {
0% { transform: translateX(0) translateZ(0) scaleY(1); }
50% { transform: translateX(-25%) translateZ(0) scaleY(0.9); }
100% { transform: translateX(-50%) translateZ(0) scaleY(1); }
}
将动画应用到 SVG 的波浪路径上,可以实现更流畅的波浪运动效果。
响应式设计
确保水波球在不同设备上正常显示:
@media (max-width: 768px) {
.wave-ball {
width: 150px;
height: 150px;
}
.progress-text {
font-size: 18px;
}
}
以上方法提供了从基础实现到使用第三方库的不同方案,可根据项目需求选择合适的方式。SVG 方案提供更多自定义灵活性,而第三方库则简化了开发流程。





