使用JavaScript获取图片的真实尺寸大小

Webkit 内核的浏览器在加载完图片之后才能获取宽和高。所以,我建议使用onload时间来获取图片的宽和高

1 var img = $("img")[0]; // Get my img elem
2 var pic_real_width, pic_real_height;
3 $("<img/>") // Make in memory copy of image to avoid css issues
4     .attr("src", $(img).attr("src"))
5     .load(function() {
6         pic_real_width = this.width;   // Note: $(this).width() will not
7         pic_real_height = this.height; // work for in memory images.
8     });

上面的代码使用内存去存储图片来计算宽与高,所以不用担心图片因为css设置而尺寸发生了变化。

参考:http://stackoverflow.com/questions/318630/get-real-image-width-and-height-with-javascript-in-safari-chrome

原文地址:https://www.cnblogs.com/deryck/p/4183766.html