我想把svg圆圈的填充颜色从一种颜色淡出到另一种颜色,就像从红色到黑色。
<svg height="100" width="100">
    <circle cx="50" cy="50" r="40" stroke="black" stroke-width="3" fill="red" fade-to="black"/>
</svg>
我要做什么来代替fade-to="black"
发布于 2022-06-30 18:13:47
基本上,您只需要用以下两种方法来更改圆的fill属性:
begin="click")
let dotJs = document.querySelector('#dotJs');
dotJs.addEventListener('click', function(e) {
  e.currentTarget.classList.toggle('active')
})circle {
  transition: 0.5s fill;
}
.active {
  fill: #000
}
#dotCSS:hover {
  fill: #000
}<p>Css hover</p>
<svg height="100" width="100">
    <circle id="dotCSS" cx="50" cy="50" r="40" stroke="black" stroke-width="3" fill="red" />
</svg>
<p>JS click</p>
<svg height="100" width="100">
    <circle id="dotJs" cx="50" cy="50" r="40" stroke="black" stroke-width="3" fill="red" />
</svg>
<p>SMIL click</p>
<svg height="100" width="100">
    <circle id="dot" cx="50" cy="50" r="40" stroke="black" stroke-width="3" fill="red" />
      <animate id="startAnimation"
               href="#dot" 
               attributeName="fill" 
               from="red" 
               to="black" 
               begin="click"
               dur="0.5s" 
               repeatCount="0"
               fill="freeze"
/>
</svg>
查看Mike Harris的帖子:切换SVG而不使用任何脚本以获得一个更高级的svg示例。
https://stackoverflow.com/questions/72807028
复制相似问题