我一直在尝试将我的 SVG 图标外部化到一个文件中,并使用<svg><use xlink:href="file.svg#icon" /></svg>
. 从理论上讲,这非常有效,但是不同的浏览器在渲染方面存在问题。<use>
当在文件内部引用符号并直接打开 svg 文件的 url时,所有浏览器都能够正确呈现 svg 。
简而言之,linearGradient
在引用标记中的符号时,是否有一种跨浏览器的方法可以让 SVG 充当元素的填充<svg><use/></svg>
?
我设置了一个 plunker 来演示这个问题: http ://plnkr.co/edit/feKvZ7?p=preview
简化后,标记如下所示:
<!DOCTYPE html>
<html>
<body>
<h1>SVG sprite test</h1>
<svg width="100" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
<use xlink:href="icon.svg#icon" />
</svg>
</body>
</html>
SVG 文件如下所示:
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
<defs>
<linearGradient id="gradient">
<stop offset="0" stop-color="black" />
<stop offset="1" stop-color="white" />
</linearGradient>
</defs>
<symbol id="icon" viewBox="0 0 100 100">
<circle cx="50" cy="50" r="40" stroke="black" fill="url(#gradient)" />
</symbol>
<use id="iconuse" xlink:href="#icon" width="100" height="100" />
</svg>