我需要一些关于如何解决这个问题的建议。
单击页面中的LInk时,我有一段要执行的Ajax代码。ajax代码用于调用PHP代码来查询某个目录中的图像(缩略图大小)文件名。一旦执行,ajax数据将在该目录中包含我的缩略图的文件名,并在我的页面中动态显示缩略图。
我想知道如何在不使用SetTimeOut()的情况下检查所有缩略图是否已经完成加载?并在加载完所有缩略图后执行一些jquery代码。我需要加载我的缩略图,这样我就可以设置我的缩略图div的大小来滚动。
发布于 2012-05-09 12:22:33
方法:
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];
}
}使用:
var imageAr = ['a.jpg', 'b.jpg', 'c.jpg'];
imagesLoaded(imageAr, function(){
alert('Images loaded!');
});发布于 2012-05-09 12:15:00
您可以对图像使用load方法,如下所示:
$('someElement img').load(function(){
//all loaded
});你的意思是这样的吗?
https://stackoverflow.com/questions/10509694
复制相似问题