加载图像的jQuery事件 [英] jQuery event for images loaded
问题描述
是否可以通过 jQuery 事件检测所有图像何时加载?
Is it possible to detect when all images are loaded via a jQuery event?
理想情况下,应该有一个
Ideally, there should be a
$(document).idle(function()
{
}
或
$(document).contentLoaded(function()
{
}
但是我找不到这样的东西.
But I can't find such a thing.
我想附加一个这样的事件:
I thought of attaching an event like this:
$(document).ready(function()
{
var imageTotal = $('img').length;
var imageCount = 0;
$('img').load(function(){if(++imageCount == imageTotal) doStuff();});
}
但是如果图像加载失败,这会中断吗?在正确的时间调用方法至关重要.
But will this break if an image fails to load? It's critically important for the method to be called, and at the right time.
推荐答案
根据 jQuery 的 文档,对图像使用加载事件有许多注意事项.如另一个答案所述,ahpi.imgload.js 插件已损坏,但不再维护链接的 Paul Irish gist.
Per jQuery's documentation, there are a number of caveats for using the load event with images. As noted in another answer, the ahpi.imgload.js plugin is broken, but the linked Paul Irish gist is no longer maintained.
Per Paul Irish,用于检测图像加载完成事件的规范插件现在位于:
Per Paul Irish, the canonical plugin for detecting image load complete events is now at:
https://github.com/desandro/imagesloaded
这篇关于加载图像的jQuery事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!