I have a script that loops through a series of JSON objects that represent images, the loop loads an image and once it has been loaded it appends it to an element on my website/application.
The images are in the correct order in the JSON however they are appearing in different orders depending on the images that load the quickest because they are appended first. I want to make sure that whatever order the images load, they are always in the correct order (The order that the loop denotes).
The method I'm hoping to end up with allow me to access the style of the wrapping anchor before the image is loaded to show a loading icon also.
Here's the part of the script that I am talking about:
$.each(project.images, function (index, image){
var image_src = '<?= IMAGE_PATH ?>library/preview/'+ image.file_name;
var image_markup = $('<img class="new-image" />').attr('src', image_src)
.load(function(){
if (this.complete)
{
var anchor_markup = $('<a href="javascript:void(0)" class="image-anchor" data-image-index="'+ index +'" id="image-index-'+ index +'">');
$(anchor_markup).append(image_markup);
globals.markup.image_slider.append(anchor_markup);
$('.new-image').fadeIn(200).removeClass("new-image");
}
});
});
The example can also be seen at http://www.staging.codebycoady.com/work