【发布时间】:2019-02-18 15:15:19
【问题描述】:
我正在尝试获取刚刚添加到body 的object 元素的getBoundingClientRect(),但它返回width 和height 0。目前我修复了将SVG 添加到包含相同图片的 html 并将可见性设置为隐藏,然后从中获取宽度和高度。对象的大小是窗口大小的百分比,所以我无法提前知道。
let bulletSVG = document.createElement("object");
bulletSVG.setAttribute("class", "bullet");
bulletSVG.setAttribute("type", "image/svg+xml");
bulletSVG.setAttribute("data", "imgs/bullet.svg");
document.body.appendChild(bulletSVG);
console.log(bulletSVG.getBoundingClientRect());
我宁愿不为 body 添加 SVG 来获取宽度和高度。我该怎么办?
【问题讨论】:
标签: javascript svg getboundingclientrect