javascript - 如何在 IE 11 的 Image.load 上获取 SVG 的宽度和高度

标签 javascript internet-explorer svg

它在任何地方都可以正常工作,但在 IE 11 中不行(我还没有测试过其他 IE 版本)。

var img = new Image();
img.onload = function(){

   alert( 'img: ' + img.width + 'x' + img.height + 
          '  natural: ' + img.naturalWidth + 'x' + img.naturalHeight );

};
img.src = 'http://upload.wikimedia.org/wikipedia/en/b/b5/Boeing-Logo.svg';

JSFiddle: <知识库> JSFiddle

结果:

img: 121x30 natural: 121x30 - 真正的浏览器(Chrome、Safari、Firefox,...)

img: 0x0 natural: 0x0 - IE 11

这里有一个类似的问题:Getting image width on image load fails on IE

这些答案中的解决方案均不适用于 svg。

有没有办法在 Internet Explorer 11 中获取使用 Image() 加载的 svg 文件的宽度和高度?

注意:我正在寻找一种无需将元素添加到 DOM 进行测量的解决方案,因为我想避免任何不必要的重排/重绘。

最佳答案

此代码适用于 IE11:

var img = new Image();
img.onload = function(){
    document.body.appendChild(this);
    alert( 'img: ' + this.offsetWidth + 'x' + this.offsetHeight);
    document.body.removeChild(this);
};
img.src = 'http://upload.wikimedia.org/wikipedia/en/b/b5/Boeing-Logo.svg';

是的 - 这个解决方案并不理想。

关于javascript - 如何在 IE 11 的 Image.load 上获取 SVG 的宽度和高度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22822921/

相关文章:

javascript - 本地 HTML 文件与(读/写)本地 .xlsm Excel 文件通信的最佳方式

html - IE 11 伪元素::之前的绝对背景

Internet Explorer 中的 CSS "unset"/"initial"

css - IE 中 xml 标准兼容模式的 Doctype

php - 使用 PHP 的 file_get_contents 后将类添加到 SVG 元素

javascript - 如何定义具有多个属性的 sequalize 表字段

javascript - 在使用 ajax 提交时,在 greasemonkey 用户脚本中将文本附加到表单

javascript - contenteditable div 中的 HTML5 可拖动元素 - 在第一次放置后停止工作 - 为什么?

使用图像标签加载 svg 时,从悬停开始的 Svg 动画不会播放

javascript - 使用 anime.js 变形 : coordinates jump somewhere else in between given start and end position