Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I would turn it into a plugin and use it this way:</p> <pre><code>$.loadImages = function($images, callback) { var imgCount = $images.length; if (!imgCount) { callback(); } else { $images.each(function () { /*$(this).one("load error", function () { imgCount--; if (imgCount == 0) { callback(); } }); if (this.complete) $(this).load();*/ var img = new Image(); $(img).on("load error", function(){ imgCount--; if (imgCount == 0) { callback(); } }); img.src = this.src; }); } }; $.fn.loadImages = function (callback) { var $images = this.find("img").addBack().filter("img"); var self = this; $.loadImages($images,function(){ if ($.isFunction(callback)) { callback(); } self.trigger("imagesLoaded"); }); }; </code></pre> <p>Now you can use it like this:</p> <pre><code>$.loadImages($("div.image-container").find("img"), function() { console.log("Images loaded"); }); </code></pre> <p>or you can use it like this:</p> <pre><code>$("div.image-container").on("imagesLoaded",function(){ console.log("Images loaded"); }).loadImages(); </code></pre> <p>or even like this:</p> <pre><code>$("div.image-container").loadImages(function(){ console.log("Images loaded"); }); </code></pre> <p>this works too:</p> <pre><code>$("div.image-container img").loadImages(function(){ console.log("Images loaded"); }); </code></pre> <p>There is no way of making .on automatically cause the plugin to run without monkeypatching .on, which is usually a bad idea. It might look something like this:</p> <pre><code>var oldOn = $.fn.on $.fn.on = function() { var ret = oldOn.apply(this,arguments); if (arguments[0] == "imagesloaded") { var self = this; onImagesLoaded(this,function(){ self.trigger("imagesloaded"); }); } return ret; }; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload