Skip to content

Instantly share code, notes, and snippets.

@mach3
Created October 19, 2012 11:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mach3/3917623 to your computer and use it in GitHub Desktop.
Save mach3/3917623 to your computer and use it in GitHub Desktop.
画像が本当に読み込まれたかどうか確認する
/**
* cf) http://dzone.com/snippets/checking-if-image-loaded
*
* 画像要素を追加した後からイベントを設定しなきゃいけないような困った時の為に。
* キャッシュから読んでたり?するとonloadが発火しなかったり
* バブリングは上手く動かなかったりしたので、画像が正常に読み込まれているかどうかをチェックしてから諸々行う。
*/
$.extend($, {
getImageLoaded : function(ele){
if(! ele.complete){
return false;
}
if(typeof img.naturalWidth !== "undefined" && img.naturalWidth === 0){
return false;
}
return true;
}
});
$.fn.extend({
onImageLoaded : function(callback){
this.each(function(){
if($.getImageLoaded(this)){
callback.call(this);
return;
}
$(this).on("load", callback);
});
return this;
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment