Get Each Image Natural Height With jQuery

后端 未结 2 1974
栀梦
栀梦 2020-12-31 00:43

I have to read each images\' natural height and I should make calculations. However I have some problem with read to natural height.

$(\'div.imgkirp img\').e         


        
相关标签:
2条回答
  • 2020-12-31 00:44
    var image = new Image();
    image.src = $(this).attr("src");
    alert('width: ' + image.naturalWidth + ' and height: ' + image.naturalHeight);
    

    This approach doesn't work in IE 8 and below versions, because it doesn't support 'naturalWidth' and 'naturalHeight' properties. To achieve the same use this code

    var image = new Image();
    image.src = $(this).attr("src");
    image.onload = function() {
    console.log('height: ' + this.height);
    };
    
    0 讨论(0)
  • 2020-12-31 00:44

    Try to use property naturalHeight of image, using the prop method of the jQuery

    $('div.imgkirp img').each(function(){
    
       console.log($(this).prop('naturalHeight'));
    });
    
    0 讨论(0)
提交回复
热议问题