我有动画 svg 文件,我想将它用作 div 的背景图像
div { background-image: url('anim.svg'); }
图像显示但动画不工作。
附言我不能在 html 中使用 svg-object,因为我有很多类似的对象,而且 svg-object 的代码并不短。
更新:svg 文件代码:
<?xml version="1.0" encoding="utf-8"?>
<svg height="69" width="62" class="hexagon" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
<polygon points="30 0, 60 16, 60 50, 30 68, 0 50, 0 16" style="fill: none; stroke:#fff;stroke-width:1" />
<polygon points="33 3, 33 3" style="fill: none; stroke:#fff;stroke-width:1">
<animate attributeName="points" dur="500ms" to="33 3, 59 48" fill="freeze" />
</polygon>
<polygon points="28 2, 28 2" style="fill: none; stroke:#fff;stroke-width:1">
<animate attributeName="points" dur="500ms" to="28 2, 0 49" fill="freeze" />
</polygon>
<polygon points="3 51, 3 51" style="fill: none; stroke:#fff;stroke-width:1">
<animate attributeName="points" dur="500ms" to="3 51, 57 51" fill="freeze" />
</polygon>
</svg>
最佳答案
您可以使用 svg:use 来重复相似的对象,例如:
<svg>
<defs>
<g id="shape">
<rect x="50" y="50" width="50" height="50" />
<circle cx="50" cy="50" r="50" />
</g>
</defs>
</svg>
此代码将呈现相同的对象:
<svg>
<use xlink:href="#shape" x="200" y="50" />
</svg>
关于CSS 背景图像 *.svg 动画不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28633655/