css - CSS/SVG 中的停止颜色不起作用

标签 css svg

所以,我有以下代码:

<?xml version="1.0" standalone="yes"?>
<svg xmlns="http://www.w3.org/2000/svg" width="360" height="240">
<style>stop{stop-opacity:1}circle{fill:url(#r)}</style>
<defs>
<radialGradient id="r" cx="50%" cy="50%" r="50%" fx="50%" fy="50%">
<stop offset="0%" style="stop-color:#ce1126"/>
<stop offset="17%" style="stop-color:#e5911d"/>
<stop offset="33%" style="stop-color:#fcd116"/>
<stop offset="50%" style="stop-color:#18c063"/>
<stop offset="67%" style="stop-color:#75aadb"/>
<stop offset="83%" style="stop-color:#803ac5"/>
<stop offset="100%" style="stop-color:#00335b"/>
</radialGradient>
</defs>
<circle cx="150" cy="300" r="55"/>
</svg>

有趣的是,如果我将 stop-color:attr(c) 添加到停靠点的 CSS 并相应地更改它们 (style="stop-color:= >c="),它不起作用(或者至少在 Safari/iOS9 中不起作用)。这是为什么?

最佳答案

有趣的是 Safari 不支持这个。这似乎在 Firefox 中有效:

<svg xmlns="http://www.w3.org/2000/svg" width="360" height="240">
  <style>
    stop {
      stop-opacity: 1
    }
    circle {
      fill: url(#r)
    }
  </style>
  <defs>
    <radialGradient id="r" cx="50%" cy="50%" r="50%" fx="50%" fy="50%">
      <stop offset="0%" style="stop-color:#ce1126" />
      <stop offset="17%" style="stop-color:#e5911d" />
      <stop offset="33%" style="stop-color:#fcd116" />
      <stop offset="50%" style="stop-color:#18c063" />
      <stop offset="67%" style="stop-color:#75aadb" />
      <stop offset="83%" style="stop-color:#803ac5" />
      <stop offset="100%" style="stop-color:#00335b" />
    </radialGradient>
  </defs>
  <circle cx="60" cy="100" r="55" fill="url(#r)" />
</svg>

我不推荐这样做。 最受支持的方式应该是:

  • 添加停止色作为属性
  • 向停止元素添加类或 ID,并使用 css 添加停止颜色。

关于css - CSS/SVG 中的停止颜色不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37869184/

相关文章:

html - Firefox,显示 : -moz-box adds weird margin on top

javascript - 使用 JavaScript 在 HTML 中显示 CSS 属性的值

css - 如何将 flag-icon-css 与 angular2/4 一起使用?

javascript - ScrollMagic,反向 z 索引顺序

javascript - 在 web View 中放置 svg 对象

javascript - 如何将带有上下文菜单的展开所有按钮添加到 d3.js 树中

jquery - ListView 在 jQuery Mobile 的网格系统中显示不正确

css - 如果复选框被选中则隐藏一个元素

html - DIV 覆盖整个页面(IE7)

javascript - 当附加到 React 中的 SVG 图标时,onClick 事件处理程序无法按预期工作