获取图像尺寸问题
我一直在尝试动态加载图像 - 动态调整大小以通过 jQuery 设置包装器 div 的高度和宽度。
但问题是 alert($(".imageContainer").height()
显示 0
因为我相信当我调用此函数时图像尚未加载。
该过程是:当用户单击网格上的缩略图时,会弹出一个模式窗口,并且一些简单的 jQuery 代码会在此模式窗口上设置 src
属性以显示相应的图像
:
$(".thumb-img").click(function(){
$(".bigImage").attr("src","somewhere/"+$(this).attr("data-imageid")+".jpg");
//needs to know whether image has been loaded or not
$(".imageContainer").height($(".bigImage").height());
$(".imageContainer").width($(".bigImage").width());
});
图像大小不是 。一致,我需要根据它们的大小动态设置大小,
该怎么做?
我
I've been trying to get image-loading dynamically- size dynamically to set wrapper div height and width via jQuery.
But the problem is alert($(".imageContainer").height()
shows 0
since I believe the images has not been loaded when I call this function.
The process is : when user click a thumbnail on a grid, a modal window pops up and and some simple jQuery code set the src
attribute on this modal window to show corresponding image.
Lets say :
$(".thumb-img").click(function(){
$(".bigImage").attr("src","somewhere/"+$(this).attr("data-imageid")+".jpg");
//needs to know whether image has been loaded or not
$(".imageContainer").height($(".bigImage").height());
$(".imageContainer").width($(".bigImage").width());
});
The images size are not consistent and I need to set size according to their size dynamically.
How can I do that?
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您想知道图像是否已加载,可以使用
load
事件(它也适用于图像):当您将与图像相关的代码放入
load< /code> 窗口通过后,所有图像和其他资源都已加载,因此您可以放心地假设所有图像均已加载:
If you want to know whether or not image has loaded, you can use the
load
event (it works for images too):When you put your image-related code in
load
of the window through, all images and other resources are loaded by then, so you can safely assume all images are loaded: