代码之家  ›  专栏  ›  技术社区  ›  will

带图像模式的SVG仅在Chrome中工作

  •  -1
  • will  · 技术社区  · 7 年前

    我正在尝试获取一个顶部有一个角度的图像,该图像需要通过CMS进行更新。我发现最兼容跨浏览器的方法是使用SVG,创建一条路径并给它一个模式。我现在的代码是:

    <svg class="workIntroImage" width="696px" height="921px" viewBox="0 0 696 921">
      <defs>
        <pattern id="workIntroImg" width="1920" x="0" y="0" height="1080" patternUnits="userSpaceOnUse">
          <image xlink:href="https://www.usmagazine.com/wp-content/uploads/2017/12/147234372_the-office-zoom.jpg" x="0" y="0" width="1920" height"1080"></image>
        </pattern>
      </defs>
      <g x="0" y="0" transform="translate(-37.000000, 0.000000)">
        <path x="0" y="0" d="M0,0 L725.886225,151.670321 C729.593802,152.445001 732.25,155.713561 732.25,159.501206 L732.25,928 L0,928 L0,0 Z" fill="url(#workIntroImg)"></path>
      </g>
    </svg>

    但它只在Chrome中工作-在Firefox、Safari和Edge中显示为空白。任何能在所有现代浏览器中工作的洞察力或替代解决方案都将受到赞赏!

    1 回复  |  直到 7 年前
        1
  •  1
  •   MTCoster    7 年前

    这可能只是一个打字错误-有一个 = 失踪 <image ... height"1080"> . Chrome擅长分析无效标记,其他浏览器并不总是如此。

    <svg class="workIntroImage" width="696px" height="921px" viewBox="0 0 696 921">
      <defs>
        <pattern id="workIntroImg" width="1920" x="0" y="0" height="1080" patternUnits="userSpaceOnUse">
          <image xlink:href="https://www.usmagazine.com/wp-content/uploads/2017/12/147234372_the-office-zoom.jpg" x="0" y="0" width="1920" height="1080"></image>
        </pattern>
      </defs>
      <g x="0" y="0" transform="translate(-37.000000, 0.000000)">
        <path x="0" y="0" d="M0,0 L725.886225,151.670321 C729.593802,152.445001 732.25,155.713561 732.25,159.501206 L732.25,928 L0,928 L0,0 Z" fill="url(#workIntroImg)"></path>
      </g>
    </svg>