如何使用jquery获取图片真实宽高

jQuery有没有获取图片实际尺寸的方法?
就是图片文件的实际尺寸,而不是添加了css样式之后的尺寸。

//code from https://www.chinaobd2.com/wholesale/sbb3-pro3-key-programmer.html
$(function(){
	var imgSrc = $("#image").attr("src");
	getImageWidth(imgSrc,function(w,h){
		console.log({width:w,height:h});
	});
});

function getImageWidth(url,callback){
	var img = new Image();
	img.src = url;
	
	// 如果图片被缓存,则直接返回缓存数据
	if(img.complete){
	    callback(img.width, img.height);
	}else{
            // 完全加载完毕的事件
	    img.onload = function(){
		callback(img.width, img.height);
	    }
        }
	
}



猜你喜欢

转载自www.cnblogs.com/aid12580/p/10674516.html
今日推荐