2016-06-16 76 views
-1

所以,我有以下代码:停止,颜色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中)。为什么是这样?

回答

1

有趣的是,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。
+0

有趣的是,作为[本网站](http://www.w3schools.com/svg/svg_grad_radial.asp)使用内联样式的方法。我会给它一个镜头。 –

+3

@SomePerson请不要使用“那个网站”使用更多的mozilla文档更新:https://developer.mozilla.org/en-US/docs/Web/SVG/Tutorial/Gradients – Persijn