简体   繁体   English

使用CSS为SVG路径元素的填充设置动画

[英]Using CSS to animate the fill of SVG path elements

I have a JSFiddle here . 这里有一个JSFiddle 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. 我使用walkway.js绘制了一个path元素的walkway.js并且效果很好,但是在绘图完成之后我想为svg的路径元素的填充设置动画。 I have tried giving the path the following CSS: 我试过给路径下面的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: 并且在回调函数上(在.draw()完成时执行)我通过应用类testClass改变路径fill的值,如下所示:

.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. 这种方法适用于普通的html元素,不涉及SVG。 I appreciate any help. 我感谢任何帮助。

Give it something to transition from, by adding fill: white to your path. 通过在路径中添加fill: white来为其提供转换。

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

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

You can't transition fill from nothing to black. 您无法将填充从无变换为黑色。 Add original fill to path 将原始填充添加到path

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

fiddle 小提琴

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

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