The radial gradient is supposed to grow from 10% to 100% on hover, but it’s just not doing anything. Can’t understand what I’m doing wrong.
<svg id="svgDoc" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" viewBox="0 0 500 500" shape-rendering="geometricPrecision" text-rendering="geometricPrecision">
<style><![CDATA[
#svgDoc {
pointer-events: all
}
#svgDoc:hover #svgGrad {
animation: svgGrad_f_p 3000ms linear 1 normal forwards
}
@keyframes svgGrad_f_p {
0% {
offset: 10%
}
100% {
offset: 100%
}
}
]]>
</style>
<defs>
<radialGradient id="svgGrad-fill" cx="0" cy="0" r="0.5" spreadMethod="pad" gradientUnits="objectBoundingBox" gradientTransform="translate(0.5 0.5)">
<stop id="svgGrad-fill-0" offset="0%" stop-color="#ff0000"/>
<stop id="svgGrad-fill-1" offset="10%" stop-color="#000"/>
</radialGradient>
</defs>
<rect id="svgGrad" width="500" height="500" rx="0" ry="0" fill="url(#svgGrad-fill)"/>
</svg>
2
Answers
You can’t manipulate the
stop
attribute via CSS as it "collides" with the CSS offset-path related property of the same name.A workaround might be to animate the gradient via SMIL animation like so:
Fortunately, SVG’s
<animate>
element allows you toadd events to start (or stop) playback. See "mdn docs: begin"
To animate the radial gradient in your SVG you need to ensure that the properties being animated are compatible with CSS animations. In SVG you cannot directly animate the offset property of gradient stops using CSS. Instead, you need to use SMIL (Synchronized Multimedia Integration Language) for animating $VG properties.
Here’s how you can modify your SVG to animate the radial gradient using SMIL: