您的位置:首页 > Web前端 > JavaScript

JavaScript获取页面图片原始尺寸

2013-11-10 14:14 260 查看


通过Image()对象获取原始宽高

这种方式就没有那么麻烦,直接new一个Image()对象,然后把img的src赋值给他即可获取。

var img = new Image();
img.src = $("#target").attr("src");
if(img.complete){
   alert('width:'+img.width+',height'+img.height);
   img = null;
}else{
   img.onload = function(){
       alert('width:'+img.width+',height'+img.height);
       img = null;
   };
}


并且不要担心new Image对象会多一个http请求,浏览器加载图片后已经有缓存,你new N个image对象都没问题,当然,内存会消耗,所以用完后img置为null。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: