Last active
August 29, 2015 14:07
-
-
Save YusukeHirao/8e944895b65cea33a72d to your computer and use it in GitHub Desktop.
その要素内の画像がロードされてからコールバックを実行するプラグイン。画像のロードを必須とする処理にラップする。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$.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