简体   繁体   English

jQuery width()和height()为img元素返回0

[英]jQuery width() and height() return 0 for img element

So I am creating a new image element once I get response from AJAX call with image metadata like this: 所以我创建一个新的图像元素,一旦我从AJAX调用响应像这样的图像元数据:

var loadedImageContainter = $('<div class="loaded-image-container"></div>');
var image = $('<img src="' + file.url + '" alt="">');
loadedImageContainter.append(image);
$('.loading-image').replaceWith(loadedImageContainter);
var width = image.width();
var height = image.height();
console.log(width);
console.log(height);

But width() and height() functions are returning 0 although image has 30 x 30 px size and it appears correctly on the page. 但是width()和height()函数返回0,尽管图像大小为30 x 30 px并且它在页面上正确显示。 I need to get its dimensions in order to absolutely position the image. 我需要获得它的尺寸才能绝对定位图像。

You need to wait until the image has loaded to have access to the width and height data. 您需要等到图像加载后才能访问宽度和高度数据。

var $img = $('<img>');

$img.on('load', function(){
  console.log($(this).width());
});

$img.attr('src', file.url);

Or with plain JS: 或者使用简单的JS:

var img = document.createElement('img');

img.onload = function(){
  console.log(this.width);
}

img.src = file.url;

Also, you don't need to insert the image into the DOM before you read the width and height values, so you could leave your .loading-image replacement until after you calculate all of the correct positions. 此外,在读取宽度和高度值之前,您不需要将图像插入DOM,因此您可以在计算所有正确位置之前保留.loading-image替换。

That is because your image isn't loaded when you check width 这是因为检查宽度时未加载图像

Try using load event this way 尝试以这种方式使用load事件

$('img').on('load',function(){
  // check width
});

您还可以使用备用API $ .load方法:

$('<img src="' + file.url + '" alt="">').load( function(){ <your function implementation> });

As @ahren said earlier the reason is that image not loaded when you tring to get it's sizes. 正如@ahren之前所说的那样,原因是当你想要获得它的大小时图像没有加载。

If you want to fix this without jQuery you can use vanilla JS addEventListener method: 如果你想在没有jQuery的情况下解决这个问题,你可以使用vanilla JS addEventListener方法:

document.getElementsByTagName('img')[0].addEventListener('load', function() {
    // ...
    var width = image.width();
    var height = image.height();
    console.log(width);
    console.log(height);
    // ...
});

For me it only works with "appendTo" 对我来说它只适用于“appendTo”

$('<img src="myImage.jpeg">').load(function(){
   $w = $(this).width(); 
   $h = $(this).height();
   $(this).remove()
}).appendTo("body")

Try this: 试试这个:

$('.loading-image').replaceWith(loadedImageContainter);
$('.loading-image').load(function(){
    var width = image.width();
    var height = image.height();
    console.log(width);
    console.log(height);
});

If you put it inside the AJAX call, that will work too, this is for json method 如果你把它放在AJAX调用中,那也可以,这适用于json方法

$.post("URL",{someVar:someVar},function(data){

   var totalImgs = $('body').find('img').length;

   var image = $('<img src="' + data.file[0].url + '" alt="" id="IMG_'+ totalImgs +'">');

   loadedImageContainter.append(image);
   $('.loading-image').replaceWith(loadedImageContainter);

   var width = $('#IMG_'+totalImgs).width();
   var height = $('#IMG_'+totalImgs).height();

},'json');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM