简体   繁体   English

强制刷新 SVG 渐变

[英]Force refresh of SVG gradient

I have an SVG which needs to be modified dynamically after page load.我有一个 SVG 需要在页面加载后动态修改。

Some elements are using fill="currentColor" and others are using a gradient fill, which uses stop-color="currentColor" in its stop elements.一些元素使用fill="currentColor"而其他元素使用渐变填充,它在其stop元素中使用stop-color="currentColor" The idea is that I should be able to simply change the color attribute of the parent SVG and all of the child elements should automatically change color too.这个想法是我应该能够简单地更改父 SVG 的color属性,并且所有子元素也应该自动更改颜色。

This works great for the elements which are using fill="currentColor" , but the gradient-filled elements are not picking up the change.这对于使用fill="currentColor"的元素非常fill="currentColor" ,但渐变填充的元素没有接受更改。

Anything I can do to force a refresh?我能做些什么来强制刷新?

https://jsfiddle.net/chrisAtFundrise/bb9c8gnh/ https://jsfiddle.net/chrisAtFundrise/bb9c8gnh/

This might be a webkit bug!这可能是一个webkit错误! Since the linearGradient element is within a defs element, and you have currentColor used on your stop element which is not being inherited.由于linearGradient元素位于defs元素内,并且您在stop元素上使用了currentColor ,而该元素未被继承。 You could also target the linearGradient children <stop> elements stop-color attribute.您还可以针对linearGradient子元素<stop>元素的stop-color属性。

https://jsfiddle.net/bb9c8gnh/10/ https://jsfiddle.net/bb9c8gnh/10/

$("#button").on("click", function() {  
    $("svg").attr({color: "blue"});
    $("svg linearGradient stop").eq(0).attr({"stop-color": "blue", offset:"100%"});
    $("svg linearGradient stop").eq(1).attr({"stop-color": "blue"});
 });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM