preload images
function preloadImages(arr) {
var newimages = [],
loadedimages = 0;
var postaction = function() {};
arr = (typeof arr != "object") ? [arr] : arr;
function imageloadpost() {
loadedimages++;
if (arr[loadedimages - 1] === null) {
log('nothing here');
}
if (loadedimages == arr.length) {
postaction(newimages);
for (var j = 0, len = arr.length; j < len; j++) {
$(arr[j]).append(newimages[j]);
$(arr[j]).removeAttr("data-src");
}
}
}
var img;
for (var i = 0, len = arr.length; i < len; i++) {
img = new Image();
newimages.push(img);
img.addEventListener("load", function() {
imageloadpost();
});
img.addEventListener("error", function() {
imageloadpost();
});
if (arr[i].getAttribute('data-src') !== null) {
img.src = arr[i].getAttribute('data-src');
img.alt = "";
}
}
return {
done: function(f) {
postaction = f || postaction;
}
};
}