一、SVG图片的优势
1. 无损压缩
2. 可无限缩放
3. 跨平台兼容性好
二、Vue3中SVG图片的巧妙应用
1. 组件图标
<template>
<svg>
<use xlink:href="#icon-name"></use>
</svg>
</template>
2. 动画效果
<template>
<svg :viewBox="`0 0 100 100`">
<circle cx="50" cy="50" r="40" fill="none" stroke="black" :stroke-width="5">
<animate attributeName="r" from="40" to="50" dur="1s" fill="freeze" />
</circle>
</svg>
</template>
3. 交互式图表
<template>
<svg :width="chartWidth" :height="chartHeight">
<circle
cx="50"
cy="50"
r="30"
fill="none"
stroke="black"
:stroke-width="5"
:fill="`url(#gradient)`"
/>
<defs>
<linearGradient id="gradient" x1="0%" y1="0%" x2="100%" y2="0%">
<stop offset="0%" stop-color="#f00" />
<stop offset="100%" stop-color="#00f" />
</linearGradient>
</defs>
</svg>
</template>
<script>
export default {
data() {
return {
chartWidth: 100,
chartHeight: 100,
};
},
};
</script>
三、SVG图片的优化技巧
1. 使用SVG压缩工具
2. 合并SVG图片
3. 使用SVG Sprites
4. 使用CSS动画代替SVG动画
如果SVG动画较为简单,可以考虑使用CSS动画代替SVG动画,提高动画性能。