【问题标题】:How do I use svg patterns in a cross browser consistent way?如何以跨浏览器一致的方式使用 svg 模式?
【发布时间】:2016-10-17 20:59:40
【问题描述】:

我想要一个 SVG 图像(预渲染,但在 svg 标签中插入 js,以允许进一步操作)能够使用预定义的模式,使用“模式”标签。听起来很简单,不是吗?好吧,事实证明 Chrome(Webkit?)的行为与任何其他浏览器都有点不同,现在我不确定实现这一目标的最佳方法是什么。

我的 svg 看起来像这样:

<svg>
 <defs>
  <pattern id="specialPattern">...</pattern>
 </defs>
 <path class="special"></path>
</svg>

我希望类 special 的路径具有“模式”作为填充。

尝试一:在 Chrome 中工作,而不是在 FF 或 Opera 中

我的第一次尝试是简单地将它放在我的 css 中:

 .special { fill:url("#specialPattern");}

这实际上在 Chrome 中有效,但当您考虑它时,它可能不应该。我尝试的其他浏览器将此 url 解释为相对于它所在的文件(css 文件)which makes more sense

尝试二:在 FF 和 Opera 中工作,而不是在 Chrome 中

下一次尝试:提供模式的绝对网址。

 .special { fill:url("//example.com/styles/svgWithStyleDeclaration.svg#specialPattern");}

虽然这在 FF 和 Opera 中按预期工作,但 Chrome 现在会重置填充(我不知道它实际上在哪里寻找那种样式)

尝试三:工作,有点

在 SVG 中内嵌样式似乎无处不在:style="fill:url('#specialPattern')"

虽然我猜这是内容和演示之间的界限模糊的情况,但至少在我的情况下,将样式声明保留在其他地方会更好(尤其是因为这会使我的 SVG 需要更大)

尝试四:工作(?)但很脏

我没有测试过很多浏览器,所以我不确定它的防水性如何,但在我看来,使用 css hack 来检测 webkit 浏览器会起作用:

@media screen and (-webkit-min-device-pixel-ratio:0) {
  .special {fill: url("#specialPattern");}
}
 .special { fill:url("//example.com/styles/svgWithStyleDeclaration.svg#specialPattern");}

现在,必须有一种更优雅的方法来解决这个问题。应该怎么做?

编辑:原来 IE 在此处的行为类似于 Chrome,因此您还需要确保 IE

【问题讨论】:

  • 嘿,你找到解决这个问题的好方法了吗?
  • @Denis 不,我实际上应该更新这个问题,因为在最近的 Chrome 版本中行为似乎已经发生了变化(尽管我还没有时间仔细研究它)。
  • 此解决方法可能对那些在 FF、svg 模式、history.pushState 组合方面遇到问题的人有用。 github.com/rexxars/react-hexagon/issues/…

标签: css svg webkit


【解决方案1】:

这是我为操纵图案和蒙版所做的 Fiddle。这是 svg xml 中的评分显示,我希望能够在评分栏上使用百分比。

小提琴http://jsfiddle.net/cnLHE/296/

通过将最后一行更改为“width="50”,然后按运行,您可以看到评级栏调整大小。

<svg width="100%" height="100%" viewBox="0 0 100 20" version="1.1">
<defs>
  <pattern id="pattern1" x="0" y="0" width="20" height="20"
         patternUnits="userSpaceOnUse" >
      <circle cx="10" cy="10" r="5" style="fill:white" />
   </pattern>
  <pattern id="pattern2" x="0" y="0" width="20" height="20"
         patternUnits="userSpaceOnUse" >
      <circle cx="10" cy="10" r="9" style="fill:white" />
      <circle cx="10" cy="10" r="7" style="fill:black" />
    </pattern>
  <mask id="mask1" x="0" y="0" width="100" height="20" >
    <rect x="0" y="0"  width="100" height="20"
        style="stroke:none; fill: url(#pattern2)"/>
  </mask>
  <mask id="mask5" x="0" y="0" width="100" height="20" >
    <rect x="0" y="0"  width="100" height="20"
        style="stroke:none; fill: url(#pattern1)"/>
  </mask>
</defs>1<rect x="0" y="0" width="500" height="20" fill="url(#pattern2)" style="fill:#2498c7; mask: url(#mask1)"/>
<rect x="0" y="0" width="50" height="20" style="fill:#2498c7; mask: url(#mask5)"/>

</svg>

我没有任何跨浏览器问题,但是,我确实遇到了 SVG 在网格布局中间歇性消失的问题。在页面中有多个实例的 webkit 中,它们并不总是显示。

更多信息请访问 css-tricks:http://css-tricks.com/using-svg/

【讨论】:

  • 是的,这将是上面的三个尝试。有效,但不完全是我想要的......
猜你喜欢
  • 2015-01-19
  • 1970-01-01
  • 2019-12-26
  • 1970-01-01
  • 2014-11-09
  • 2010-11-29
  • 1970-01-01
  • 2012-09-10
  • 1970-01-01
相关资源
最近更新 更多