Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

jquery获取图片真实宽高 #22

Open
Tronzo opened this issue Nov 30, 2018 · 0 comments
Open

jquery获取图片真实宽高 #22

Tronzo opened this issue Nov 30, 2018 · 0 comments

Comments

@Tronzo
Copy link
Owner

Tronzo commented Nov 30, 2018

$(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);
	    }
        }
	
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant