2010-04-21 36 views
1

我exteranlly加载图像通过像这样的AJAX加载的图像的高度和宽度...无法获得阿贾克斯 - 通过jQuery

function load_image(image_href) { 

    var img = new Image(); 
    $(img).load(function() { 
     $(this).hide(); $('#a_box').append(this); 
     $(this).fadeIn(); 
     }, gallery_image_load_complete() 
    ).error(function() { 
    }).attr('src', image_href); 

} 

function gallery_image_load_complete() { 
    conslole.log('complete') 

    $('#a_box img').height(); //wrong numbers, as though the image is partially loaded 
    $('#a_box img').width(); //wrong numbers 
} 

问题是,我试图让加载的图像的高度和函数gallery_image_load_complete()中的宽度。 由于某些原因,此图像高度和宽度已关闭,这是因为图像尚未完全加载。

有人可以帮我吗?

+0

负载方法的第一个参数应该是URL,这似乎缺少。可以解释这个问题。 – JohnFx 2010-04-21 22:09:23

回答

3

gallery_image_load_complete()需要在加载事件处理函数中调用:

function load_image(image_href) { 

    var img = new Image(); 
    $(img).load(function() { 
     $(this).hide(); $('#a_box').append(this); 
     $(this).fadeIn(); 
     gallery_image_load_complete(); // now inside the load event handler 
    }).error(function() { 
    }).attr('src', image_href); 

} 
2

你知道高度和宽度属性告诉你在DOM中的图像对象的大小,而不是底层的图像文件本身,对吗?

+0

但我没有调整图像大小,没有CSS,脚本或任何限制。它的高度应该是它的实际高度。 – Smickie 2010-04-21 21:50:50

1

我现在没有代码,但是当我需要任意图像的大小以决定如何呈现它时,我预先载入一个隐藏的元素并获取元素@的大小。然后做你想做的事,并把图像放在需要的地方。

效果很好。