对象的 SVG 淡化颜色

标签 svg fade

我想将 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"

最佳答案

基本上你只需要改变圆的 fill 属性:

  • CSS(例如:悬停)
  • svg SMIL 动画(使用 begin="click")
  • JavaScript(例如点击 eventListner)

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' post: Toggling SVG without any scripting一个更高级的 svg 示例。

关于对象的 SVG 淡化颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72807028/

相关文章:

javascript - SVG:在父变换上反向子旋转

html - 更改按钮内图标的边框颜色

jquery - 同时淡出多个元素 - jquery

html - 淡化背景但不淡化文本

javascript - 使用 JqueryUI 拖动内联 SVG

javascript - 更改 SVG 填充循环

javascript - 给定 X 文件数组绘制 X 图表 + 使用数组 + d3 中的元素添加图表标题

javascript - 单击时如何使div淡出?

css - 图片 :hover transition not matching given variable

objective-c - 滚动 MKMapView 时淡出弹出窗口