Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using CSS to animate the fill of SVG path elements

Tags:

html

jquery

css

svg

I have a JSFiddle here. I have animated the drawing of a path element using walkway.js and it works great, but after the drawing is completed I would like to animate the fill of the svg's path element. I have tried giving the path the following CSS:

path { 
transition: fill 2.0s linear !important;
}

and on the callback function (executed when the .draw() is completed) I change the value of the path's fill by applying the class testClass like so:

.testClass {
    fill:black;
}

This isn't working - the transition is not being applied and when the callback is called it instantly 'flicks' black-- no fade in at all. This same method has worked on plain html elements, not involved with SVGs. I appreciate any help.

like image 839
Jared Avatar asked Dec 05 '14 01:12

Jared


2 Answers

Give it something to transition from, by adding fill: white to your path.

path {
    fill: white;
    transition: fill 2.0s !important;
    }

http://jsfiddle.net/yh2jzoxa/4/

like image 143
Mark Wade Avatar answered Nov 07 '22 07:11

Mark Wade


You can't transition fill from nothing to black. Add original fill to path

path { 
    transition: fill 2.0s !important;
    fill: transparent;
}

fiddle

like image 40
Leo Avatar answered Nov 07 '22 08:11

Leo