2016-01-28 80 views
0

当我试图让图像的大小的话,我可以追加格的大小相同,解释图像。图像即将动态显示,因此如何获取浏览器中显示的图像大小而不是图像的实际大小。我已经尝试了以下代码,但在父类更小的图像时不起作用。获取动态图像的宽度

$('img').each(function(i) { 
    $currentImg = $(this); 
    imgTextshow = $currentImg.attr('imgtext'); 
    var img = new Image(); 
    img.onload = function() { 
    $currentImg.after("<div class='img-fscreen' style='width:" + this.width + "px; max-width:" + $currentImgParent + "'>" + imgTextshow + "</div>"); 
    } 
    img.src = $(this).attr('src'); 
}); 
+0

尝试使用 - 'this.naturalWidth' –

+0

@vikrantsingh,OP是询问有关图像元素的宽度,浏览器呈现后.. – Rayon

回答

0

如果使用下面的代码行onload处理的内部,他们应该给你正确的尺寸:

$currentImg.width(); 
$currentImg.height(); 

仅仅是因为这些方法返回的IMG DOM元素的宽度/高度。

0

只要使用下面一行

img.onload = function() { 
    $currentImg.after("<div class='img-fscreen' style='width:" + $currentImg.width() + "px; max-width:" + $currentImgParent + "'>" + imgTextshow + "</div>"); 
}