Answers for "Get image dimensions with Javascript before image has fully loaded"

1

Get image dimensions with Javascript before image has fully loaded

<div id="info"></div>
<img id="image" src="http://domain.com/images/example-source.jpg">

  
<script>
getImageSize('#image', function(width, height) {
    $('#info').text(width + ',' + height);
});

function getImageSize(img, callback) {
    var $img = $(img);

    var wait = setInterval(function() {
        var w = $img[0].naturalWidth,
            h = $img[0].naturalHeight;
        if (w && h) {
            clearInterval(wait);
            callback.apply(this, [w, h]);
        }
    }, 30);
}
</script>
Posted by: Guest on October-14-2021

Code answers related to "Get image dimensions with Javascript before image has fully loaded"

Code answers related to "Javascript"

Browse Popular Code Answers by Language