Last active
September 12, 2017 14:04
-
-
Save sp1r1don/7bd6a08e5da05c055294293cf7abd913 to your computer and use it in GitHub Desktop.
AJAX image load for masonry.desandro
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.imagesLoaded = function () { | |
// get all the images | |
let $imgs = this.find('img[src!=""]'); | |
// if there's no images return resolved promise | |
if (!$imgs.length) {return $.Deferred().resolve().promise();} | |
// for each image, add a deferred object to the array | |
let dfdarr = []; | |
$imgs.each(function(){ | |
let dfd = $.Deferred(); | |
dfdarr.push(dfd); | |
let img = new Image(); | |
img.onload = function(){dfd.resolve();} | |
img.onerror = function(){dfd.resolve();} | |
img.src = this.src; | |
}); | |
// return a master promise object | |
return $.when.apply($,dfdarr); | |
} | |
//usage | |
$.get(..., function(data){ | |
let $grid = $('.grid'); | |
let $data = $( data ); | |
$data.imagesLoaded().then(function(){ | |
$grid.append($data).masonry('appended', $data, true); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thx, realy good solution