我想使用方法获取BBox()用于获取 SVG 中创建的元素的宽度和高度
在这里我提供了我的代码,它在 chrome 中给出结果,但在 firefox 中不给出结果
请帮我解决一下。。
try {
console.log(document.getElementById("rect1").getBBox());
console.log(document.getElementById("rect2").getBBox());
} catch (e) {
console.log(e);
}
svg {
border: 1px dashed blue;
}
#rect2 {
display: none;
}
<svg width="300" height="200" style="border:1px dashed blue">
<rect id="rect1" width="50" height="50" fill="steelblue"></rect>
<rect id="rect2" width="50" height="50" fill="blue" x="100"></rect>
</svg>
这是因为通过使用display: none
SVG 未渲染。你应该在你的 CSS 中使用visibility: hidden
或者在调用之前检查 JS 中的 rect 样式getBBox()
.
console.log(document.getElementById("rect1").getBBox());
console.log(document.getElementById("rect2").getBBox());
svg {
border: 1px dashed blue;
}
#rect2 {
visibility: hidden;
}
<svg width="300" height="200" style="border:1px dashed blue">
<rect id="rect1" width="50" height="50" fill="steelblue"></rect>
<rect id="rect2" width="50" height="50" fill="blue" x="100"></rect>
</svg>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)