How can I get the image size before it loads in html?

First, the height of the image is zero, after which refreshing the page, I get its actual height. Do I need to get his height for the first time? can any body help me?

$j(document).ready(function(){
           var imgV = new Image();
           imgV.src="http://www.kerrvilletexascvb.com/Riverside%20Nature%20Center3.JPG";
           $j("#imgD").html(imgV.height);
           alert(imgV.height);
});


<div id="imgD"></div>
<img src="http://www.kerrvilletexascvb.com/Riverside%20Nature%20Center3.JPG" />
+3
source share
2 answers

You do not need to embed the image in the DOM to get its dimensions:

$("<img />")
    .attr("src", "http://www.google.com/intl/en_ALL/images/logo.gif")
    .load(function() {
        alert(this.width);
        alert(this.height);
    })
;
+9
source

Image size is only available when uploading an image. It’s likely that when you reload the page, the image is instantly delivered from the cache, so its size is instantly available.

- , , . onload, , - , .

div 1x1 : , - .

+1

Source: https://habr.com/ru/post/1705765/


All Articles