开发者

find image height when before loaded image [closed]

开发者 https://www.devze.com 2023-01-02 22:19 出处:网络
Closed. This qu开发者_Go百科estion does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed. This qu开发者_Go百科estion does not meet Stack Overflow guidelines. It is not currently accepting answers.

Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist

Closed 8 years ago.

Improve this question

i have one problem. now i dynamically create a div for displaying images based on image width and height. so how can i find image's width and height before loading images or using image src? help plz


I noticed in Firefox that if an img is not absolute postion, the parent div of the img is already stretched to the size of the unloaded embedded img, like when you see the alt text (if set). Maybe if you appended an img to a div with no padding for just a nanosecond, retrieve the height of the div and then remove the div.


Unfortunately, you cant know the size of an image before you load it ... (it makes sense really)


I think you can not make it just with javascript. You need some server side script to make it possible. Can you explain your purpose for getting width and height? Actually, you can change the width and height after image loading.


One option would be to create a server script that serves your images and change it so that it returns image size if certain parameter is passed to it. Then use ajax to call it before you load an image.


You can load the image on your page (hidden), then inspect the naturalWidth and naturalHeight properties, but I don't think this works in all browsers (I know it works in Firefox)

However it looks like this person figured out a way to use an *.htc file and IE's behavior rules to obtain the naturalWidth/naturalHeight in IE.


Whenever I need to do something like this, I display it with position: absolute; left: -10000px; Then I can inspect the height and width, create the div and detach / attach it to the proper place.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号