简体   繁体   English

无需第三方库即可对SVG属性进行动画处理,例如填充颜色

[英]Animate SVG property such as fill color without third-party library

How can I make the fill color of an SVG circle animate once from a given start (opaque black) to a given end (transparent/none). 如何使SVG circle的填充颜色从给定的起点(不透明的黑色)到给定的终点(透明/无)设置一次动画。 This should be triggered by a JavaScript function and the animation is a one-shot lasting a few hundred milliseconds. 这应该由JavaScript函数触发,并且动画是一次持续几百毫秒的动作。 How can I do this with DOM/CSS without having to draw in a third-party library such as Snap.svg? 我如何使用DOM / CSS做到这一点而不必绘制Snap.svg之类的第三方库?

So when my callback is invoked 所以当我的回调被调用时

  • set immediately fill: black 设置立即fill: black
  • transition over period of 200ms to fill: none 在200ms的时间内过渡以fill: none

Using pure JS you can achieve this. 使用纯JS可以实现此目的。 This thread has a function that will do what you're asking for: Animate without JQuery . 该线程具有将满足您要求的功能: 不使用JQuery进行动画处理

The function takes the element, the attribute to animate, and the speed in ms to perform the animation. 该函数采用元素,要设置动画的属性以及以毫秒为单位的速度来执行动画。 In your case something like this would be appropriate animate(circle, fill, 300) . 在您的情况下,这样的animate(circle, fill, 300)是适当的animate(circle, fill, 300) You'll likely have to alter it a bit to adapt to the alpha value decreasing, but this is the idea. 您可能必须对其进行一些更改以适应降低的alpha值,但这就是这个主意。

Try the <animate> function. 尝试<animate>函数。 Here's a demo that doesn't have external library. 这是一个没有外部库的演示

You can trigger the animation from this SO answer 您可以从该SO答案触发动画

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

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