Using CSS to animate the fill of SVG path elements

2019-04-28 16:02发布

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.

2条回答
女痞
2楼-- · 2019-04-28 16:06

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/

查看更多
老娘就宠你
3楼-- · 2019-04-28 16:24

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

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

fiddle

查看更多
登录 后发表回答