Safari foreignObject getBBox错误

阿拉·叶里斯提安

在Safari中,foreignObject元素的getBBox始终对x返回0,对于y返回0。请参见以下示例(在野生动物园中打开)。通知console.log(document.querySelector('foreignObject').getBBox());打印

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

在chrome和firefox中,它可以按预期工作。有没有解决此问题的方法?

罗伯特·朗森

将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>

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章