Skip to content

Instantly share code, notes, and snippets.

@YusukeHirao
Last active August 29, 2015 14:07
Show Gist options
  • Save YusukeHirao/8e944895b65cea33a72d to your computer and use it in GitHub Desktop.
Save YusukeHirao/8e944895b65cea33a72d to your computer and use it in GitHub Desktop.
その要素内の画像がロードされてからコールバックを実行するプラグイン。画像のロードを必須とする処理にラップする。
$.fn.imgLoaded = function (callback) {
return this.each(function() {
var self = this;
var $this = $(self);
var manifest = [];
var $imgs = $this.find('img');
$imgs.hide();
$imgs.each(function() {
var loaded = $.Deferred();
var img = new Image();
img.onload = function() {
loaded.resolve();
};
img.src = this.src;
manifest.push(loaded.promise());
});
$.when.apply($, manifest).done(function() {
$imgs.show();
callback.call(self);
});
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment