简体   繁体   English

SVG 线性渐变在 Safari 中不起作用

[英]SVG Linear gradient doesn't work in Safari

I've got an SVG object that contains a linear gradient embedded directly in a document.我有一个 SVG 对象,它包含直接嵌入在文档中的线性渐变。 It works fine in Chrome and Firefox, but in Safari nothing is rendered.它在 Chrome 和 Firefox 中运行良好,但在 Safari 中没有任何渲染。 If I create the SVG as a file and embed it using the Object tag, it works fine in Safari.如果我将 SVG 创建为文件并使用 Object 标签嵌入它,它在 Safari 中工作正常。 Other shapes and fills work, it's just linear gradient that doesn't work.其他形状和填充有效,只是线性渐变无效。 I guess I could use the object, but I'd prefer to embed the SVG directly.我想我可以使用该对象,但我更喜欢直接嵌入 SVG。

I've create a demo here (works in Chrome, not Safari): http://jsfiddle.net/sjKbN/我在这里创建了一个演示(适用于 Chrome,而不是 Safari): http : //jsfiddle.net/sjKbN/

I came across this answer which suggests setting the content type to application/xhtml+xml , but this in itself seems to cause other problems.我遇到了这个答案,它建议将内容类型设置为application/xhtml+xml ,但这本身似乎会导致其他问题。

Just wondering if anyone had come across any other fixes or ideas to get this working.只是想知道是否有人遇到过任何其他修复或想法来使其正常工作。

Your gradient will work in Safari if you wrap a defs tag around it:如果你在它周围包裹一个defs标签,你的渐变将在 Safari 中工作:

<svg version="1.1" id="Layer_1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px"
     width="300px" height="300px" viewBox="0 0 300 300" enable-background="new 0 0 300 300" xml:space="preserve">
 <defs>
<linearGradient id="SVGID_1_" gradientUnits="userSpaceOnUse" x1="5.6665" y1="149.5" x2="293.333" y2="149.5">
    <stop  offset="0" style="stop-color:#FFF33B"/>
    <stop  offset="0.0595" style="stop-color:#FFE029"/>
    <stop  offset="0.1303" style="stop-color:#FFD218"/>
    <stop  offset="0.2032" style="stop-color:#FEC90F"/>
    <stop  offset="0.2809" style="stop-color:#FDC70C"/>
    <stop  offset="0.6685" style="stop-color:#F3903F"/>
    <stop  offset="0.8876" style="stop-color:#ED683C"/>
    <stop  offset="1" style="stop-color:#E93E3A"/>
</linearGradient>
</defs>
<rect x="5.667" y="5.333" fill="url(#SVGID_1_)" width="287.667" height="288.333"/>
</svg>

​It seems that wrapping your references in defs is encouraged but not obligatory according to spec .根据规范,似乎鼓励将您的引用包装在defs中,但不是强制性的。 So this is a bug in Safari.所以这是Safari中的一个错误。

About Alpha : It seems that Safari (7 at this moment) does not cover SVG color alpha channel, use stop opacity attribute.关于 Alpha :似乎 Safari(此时为 7)不覆盖 SVG 颜色 alpha 通道,使用停止不透明度属性。 it works fine!它工作正常!

<stop stop-color="rgba(x,y,z,0.5)"> //safari does not work
<stop stop-color="rgb(x,y,z)" stop-opacity="0.5"> //ok

The accepted answer was not the solution for me.接受的答案对我来说不是解决方案。

My problem was the presence of a <base href="/" /> tag in my index file.我的问题是我的索引文件中存在<base href="/" />标记。 Simply removing it solved the problem for me.只需将其删除即可为我解决问题。

If you cannot remove it, probably some workaround already exist: found this gist but I did not tested it.如果您无法删除它,则可能已经存在一些解决方法:找到了这个要点,但我没有对其进行测试。

Update更新

Simply removing the href broke the child routing of my angular app, the proper workaround is to prepend to the linearGradient id with the page relative location.简单地删除 href 破坏了我的 angular 应用程序的子路由,正确的解决方法是在 linearGradient id 前面加上页面相对位置。 I wrapped the logic in a method like this:我将逻辑包装在这样的方法中:

get svgFill(): string {
  return `url(${this.location.path()}#${this.gradientId}) white`;
}

答案很简单,对于所有 SVG 文件,所有 id(不仅是<linear gradient> )都需要唯一。

I had some troubles too making an inline SVG with a linear gradient work.我在制作具有线性渐变的内联 SVG 时也遇到了一些麻烦。 The designer had put a - in the id of the <linearGradient .设计者在<linearGradient的 id 中加入了- The solution was as simple as removing it.解决方案就像删除它一样简单。

<linearGradient id="linear-gradient">
...
<path fill="url(#linear-gradient)" d="..."/>

with

<linearGradient id="lineargradient">
...
<path fill="url(#lineargradient)" d="..."/>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM