[英]How could you change colours of svg elements from an included file that is gotten using background-image: url(“file-name.svg”);
大家好,我有一堆包含 SVG 图像的 div
background-image : url("svg-file-path.svg");
和样本 SVG 文件
<svg xmlns="http://www.w3.org/2000/svg" width="42" height="30" viewBox="0 0 42 30">
<g fill="none" fill-rule="evenodd">
<g fill="#B4C9FF" stroke="#2C2C2C" stroke-width=".5">
<path d="M954.263 146.25c-.368 3.826-.551 8.367-.551 13.625 0 5.292.186 10.585.558 15.879h0-41.004c.374-6.012.56-11.301.56-15.868 0-4.547-.184-9.092-.555-13.636h0z" transform="translate(-913 -146)"/>
</g>
</g>
</svg>
我需要在单击某些按钮时从 javascript 更改 SVG 图像的颜色。
应用 CSS 过滤器,该过滤器引用带有 feColorMatrix 的 SVG 过滤器。 通过矩阵的第五列指定您想要的颜色。 例如,如果您想要 rgb(50,10,180),通过除以 255(50/255、10/255、180/255)来统一这些值,您将得到以下过滤器:
CSS
.icon {
filter: url(#recolorme);
}
HTML
<svg height="0px" width="0px">
<defs>
#ffff00
<filter id="recolorme" color-interpolation-filters="sRGB">
<feColorMatrix type="matrix" values="0 0 0 0 0.196
0 0 0 0 0.039
0 0 0 0 0.706
0 0 0 1 0"/>
</filter>
</defs>
</svg>
这也可以完全在 css 中完成。
将规则filter: myStuffHere
添加到带有背景图像的 css class 中。 您可以在此处使用此生成器选择任何颜色
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.