【问题标题】:Safari foreignObject getBBox is wrongSafari foreignObject getBBox 错误
【发布时间】:2019-11-11 05:30:23
【问题描述】:

在 Safari 中,foreignObject 元素的 getBBox 总是为 x 返回 0,为 y 返回 0。请参阅以下example(在 safari 中打开)。通知console.log(document.querySelector('foreignObject').getBBox()); 打印

{
  height: 100,
  width: 100,
  x: 0,
  y: 0
}

在 chrome 和 firefox 中,它按预期工作。 这个问题有解决办法吗?

【问题讨论】:

    标签: svg safari


    【解决方案1】:

    将 foreignObject 包裹在 <g> 中并获取包裹器的边界框。

    console.log(document.querySelector('.foreignObjectWrapper').getBBox());
    console.log(document.querySelector('#rect').getBBox());
    <!DOCTYPE html>
    <html>
    <head>
      <meta charset="utf-8">
      <meta name="viewport" content="width=device-width">
      <title>JS Bin</title>
    </head>
    <body>
       <svg viewBox="0 0 200 200" style="height: 200px; width: 200px;" xmlns="http://www.w3.org/2000/svg">
        <rect width="100%" height="100%" fill="red"/>
        <rect id="rect" x="50" y="50" width="100" height="100" fill="green"/>
         <g class="foreignObjectWrapper">
        <foreignObject x="50" y="50" width="100" height="100">
          <div style="background-color: yellow; width: 100px; height:100px;">
          </div>
        </foreignObject>
         </g>
      </svg>
    </body>
    </html>

    【讨论】:

      猜你喜欢
      • 2020-09-05
      • 2016-12-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多