首页 > Web开发 > 详细

JS获取图片的原始宽度和高度,兼容IE7,8

时间:2019-05-28 16:31:32      阅读:147      评论:0      收藏:0      [点我收藏+]

naturalWidth和naturalHeight 可以直接获取img的原始宽高,而innerHight,innerWith只是获取图片所占容器盒子的宽高。

// 封装
function
getNaturalSize (DomElement) { var natureSize = {}; if(window.naturalWidth && window.naturalHeight) { natureSize.width = DomElement.naturalWidth; natureSizeheight = DomElement.naturalHeight; } else { var img = new Image(); img.src = DomElement.src; natureSize.width = img.width; natureSizeheight = img.height; } return natureSize; } // 使用 var natural = getNaturalSize (document.getElementById(‘img‘)), natureWidth = natural.width, natureHeight = natural.height;

 

JS获取图片的原始宽度和高度,兼容IE7,8

原文:https://www.cnblogs.com/panax/p/10937853.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!