如何使用jQuery异步加载图像,以及如何知道图像加载完成的时间?
答
在我们的基本版本中,我们将有一个包含装载微调单格,一旦我们的大图像加载(后台),我们将删除微调,并插入我们的形象。
你知道当图像已被成功加载,当exectution达到功能在success
选项指出,如:
$.ajax({
url: 'image-map.php',
data: 'img=' + i.src, // the image url links up in our fake database
dataType: 'html',
success: function (html) {
// because we're inside of the success function, we must refer
// to the image as 'img' (defined originally), rather than 'this'
$('#loader')
.removeClass('loading')
.append(img)
.append(html);
// now show the image
$(img).fadeIn();
}
});
答
的jQuery有可用于通知您的负载方法当图像下载时。所以在你的情况下,你会使用jquery来创建一个ajax请求,它会返回一个src到图像,然后创建一个新的图像对象并应用下面的load方法。
var $img = $('<img>');
$img.attr('src', 'image.jpg').load(function() {
alert('Image Loaded');
//use jquery here to insert your image into the DOM
});
我希望这是明确的。我假设你知道如何提出一个Ajax请求,如果你问图像加载。
感谢 尼克
+0
也有是一个伟大的页面在这里,有一些优秀的jQuery技巧和在那里我最初读到使用图像的加载方法 http://www.tvidesign.co .UK /博客/改进,你-jQuery的25个优秀,tips.aspx – screenm0nkey 2009-12-19 21:03:05
感谢GTFM)) – Roman 2009-12-19 15:20:15