首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >jQuery:如何在ajax中检查图像是否已完成加载

问jQuery:如何在ajax中检查图像是否已完成加载
EN

Stack Overflow用户
提问于 2012-05-09 12:09:01
回答 2查看 1.6K关注 0票数 1

我需要一些关于如何解决这个问题的建议。

单击页面中的LInk时,我有一段要执行的Ajax代码。ajax代码用于调用PHP代码来查询某个目录中的图像(缩略图大小)文件名。一旦执行,ajax数据将在该目录中包含我的缩略图的文件名,并在我的页面中动态显示缩略图。

我想知道如何在不使用SetTimeOut()的情况下检查所有缩略图是否已经完成加载?并在加载完所有缩略图后执行一些jquery代码。我需要加载我的缩略图,这样我就可以设置我的缩略图div的大小来滚动。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-05-09 12:22:33

方法:

代码语言:javascript
复制
function imagesLoaded(imgAr, callback){
    var 
        // This is a copy of the array of images
        imageAr = imgAr.slice(0),
        // This holds the javascript Image Objects
        images = [],
        // This is the number of Images that has loaded
        loadedCount = 0;

    var imageLoaded = function(){
        // An image has finished loading, so increment the number of images loaded by 1
        loadedCount++;
        if(loadedCount>=imageAr.length){
            // If all images have finished loading, run the 'callback' function, to report back that images have all loaded
            callback.call();
        }
    }

    // Loop around each image url
    for(var i=0;i<imageAr.length;i++){
        // For each image, create a new object
        images[i] = new Image();
        // when the image finishes loading, call the imageLoaded function
        images[i].onload = imageLoaded;
        // Assign the image the appropriate src attribute
        images[i].src = imageAr[i];
    }
}

使用:

代码语言:javascript
复制
var imageAr = ['a.jpg', 'b.jpg', 'c.jpg'];

imagesLoaded(imageAr, function(){
    alert('Images loaded!');
});
票数 1
EN

Stack Overflow用户

发布于 2012-05-09 12:15:00

您可以对图像使用load方法,如下所示:

代码语言:javascript
复制
$('someElement img').load(function(){
  //all loaded
});

你的意思是这样的吗?

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/10509694

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档