jquery获取图像大小 [英] jquery get image size

查看:92
本文介绍了jquery获取图像大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用jquery创建照片库。我正在拍摄并调整负载大小以创建缩略图。我想获得图像大小的原始值,以便稍后我可以将其恢复到原始大小。有人知道怎么做吗?我有以下代码:

I am creating a photo gallery using jquery. I am taking and resizing the images on load to create thumbnails. I want to get the original value of the image's size so that later on I can take it back to its original size. Anyone know how to do this? I have the following code:

    obj.find("img").each(function(){
}); 

这将遍历容器div中的所有图像。然后我试着这样做:

This loops through all the images within the container div. I then tried to do:

$(this).width(); //didnt work
this.width; //didnt work

任何想法?

推荐答案

我问过 同样的问题问题 最近得到了一个很好的答案:创建了一个新的图像元素,但没有附加到DOM。它的高度和宽度是图像的原始尺寸。

I asked the same question recently and got a good answer: A new image element is created but not attached to the DOM. Its height and width are the original dimensions of the image.

编辑:我现在按照评论中的承诺编辑了我的问题的答案。图像大小的检测现在绑定到图像的 onload 事件,以保证可靠的结果。

Edit: I edited the answer to my question now as promised in the comments. Detection of the image size is now bound to the onload event of the image to guarantee reliable results.

这篇关于jquery获取图像大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆