【问题标题】:How to animate an SVG's paths fill-opacity property?如何为 SVG 路径填充不透明度属性设置动画?
【发布时间】:2019-08-11 18:38:53
【问题描述】:

我有以下仪表组件:

组件也在此处渲染:https://codesandbox.io/s/react-example-e3nxg

组件将 rating 作为一个数字。

我想学习如何为路径的不透明度值设置动画。例如,如果为 rating 传递了一个值 5... 如何使组件在每个路径中动画,在每个路径之间的设定时间延迟上一次一个。

关于如何做到这一点的任何想法?

【问题讨论】:

  • 可能带有 SMIL 动画标签
  • Chrome 45 弃用 SMIL,支持 CSS 动画和 Web 动画
  • codesandbox.io/s/react-example-1trl5 你能检查一下吗,这可能对你有帮助,或者这就是你正在研究的内容
  • @AnApprentice 然后他们取消了该弃用。
  • @AnApprentice 我做了一点更新,你现在能检查一下沙箱吗? codesandbox.io/s/react-example-1trl5

标签: javascript css reactjs svg


【解决方案1】:

请查看沙盒网址 Sandbox link

我对css做了一点改动

  #gauge {
  fill: red;
}

.Animate-Draw {
  fill-opacity: 0;
  animation-timing-function: ease-in;
  animation-fill-mode: forwards;
  animation-name: FillIn;
  animation-duration: 4s 0.5s;
  /* animation-delay: .5s; */
}
.Animate-Draw:nth-child(1) {
  animation-delay: 0.5s;
}
.Animate-Draw:nth-child(2) {
  animation-delay: 1s;
}
.Animate-Draw:nth-child(3) {
  animation-delay: 1.5s;
}
.Animate-Draw:nth-child(4) {
  animation-delay: 2s;
}
.Animate-Draw:nth-child(5) {
  animation-delay: 2.5s;
}
.Animate-Draw:nth-child(6) {
  animation-delay: 3s;
}
.Animate-Draw:nth-child(7) {
  animation-delay: 3.5s;
}
.Animate-Draw:nth-child(8) {
  animation-delay: 4s;
}

@keyframes FillIn {
  from {
    fill-opacity: 0;
  }
  to {
    fill-opacity: 1;
  }
}

我希望这就是你要找的。​​p>

【讨论】:

  • 感谢分享 - 这很有趣,但这里的问题是所有路径都应该始终显示,我们想要动画的是 opacity 属性。
  • 想法是,当 UIC 首次加载时,所有路径都以 0.2 的不透明度显示。然后如果评级为 2,则前 3 个项目 (0,1,2) 动画在,一次一个到完全不透明。你的解决方案有可能吗?
  • 让我试试,看起来带有 svg 的 css 动画很有挑战性
猜你喜欢
  • 1970-01-01
  • 2017-07-29
  • 2018-10-02
  • 2015-12-17
  • 2020-03-14
  • 2015-10-01
  • 1970-01-01
  • 2017-08-14
  • 2019-12-16
相关资源
最近更新 更多