检测图像加载 [英] Detect image load

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

问题描述

使用jQuery加载图像后是否可以检测?

Is it possible to detect once an image has been loaded with jQuery?

推荐答案

您可以使用 .load() 事件处理程序,如下所示:

You can use the .load() event handler, like this:

$("#myImg").load(function() {
  alert('I loaded!');
}).attr('src', 'myImage.jpg');

请务必在设置源之前 附加它,否则在附加处理程序以监听它之前可能已经触发了该事件(例如,从缓存加载).

Be sure to attach it before setting the source, or the event may have fired before you attached a handler to listen for it (e.g. loading from cache).

如果这不是 可行的(绑定后设置src),请确保检查它是否已加载并自行启动,如下所示:

If that's not doable (setting the src after binding), be sure to check if it's loaded and fire it yourself, like this:

$("#myImg").load(function() {
  alert('I loaded!');
}).each(function() {
  if(this.complete) $(this).load();
});

这篇关于检测图像加载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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