加载图片的jQuery事件 [英] jQuery event for images loaded

查看:149
本文介绍了加载图片的jQuery事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以检测所有图像是否通过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要点已经不复存在了。

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆