jQuery 图像加载后获取图像高度
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14134393/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get image height after image has loaded
提问by user198989
I am trying to get image height after image has loaded. The reason for that is chrome gets false height values because it gets height before image has loaded.
我试图在图像加载后获取图像高度。原因是 chrome 得到错误的高度值,因为它在图像加载之前获取高度。
var imgHeight = $("#pic25").height();
Note: I shouldn't use $(window).load(function(){
on this one.
注意:我不应该$(window).load(function(){
在这个上使用。
How can I do that ?
我怎样才能做到这一点 ?
回答by Arvind Bhardwaj
$("img#pic25").load(function(){
var height = $(this).height();
});
回答by NullPoiиteя
you need to use onload(.load()
) event since load event will be called only after the DOM and associated resources like images got loaded, so it work properly
您需要使用 onload( .load()
) 事件,因为只有在 DOM 和图像等相关资源加载后才会调用 load 事件,因此它可以正常工作
$(body).load(function(){
var height = $("#pic25").height();
});
this would work
这行得通
to check particular image to be load you can do this like @arvind answers code
要检查要加载的特定图像,您可以像@arvind 回答代码一样执行此操作