码迷,mamicode.com
首页 > 编程语言 > 详细

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

时间:2014-12-25 01:23:16      阅读:281      评论:0      收藏:0      [点我收藏+]

标签:

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

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

标签:

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!