I have some articles, each of them containig a list with multiple list items. Every list item has a background-image defined. The HTML looks something like this:
<article>
<ul>
<li><!-- background-image--></li>
<li><!-- background-image--></li>
</ul>
</article>
I would like to hide those list items until every image is loaded and im trying to do so for each article.
My first attempt was to use two nested loops but it feels a bit akward and complicated.
function preloader() {
$('article').each( function(i) {
$(this).addClass('loading');
var childNum = $(this).find('ul li').size();
$(this).find('ul li').each( function(n) {
var bgurl = $(this).css('background-image').replace(/url\(|\)|"|'/g,'');
$('<img/>').attr('src', bgurl).load(function() {
if (n == childNum) {
// remove class .loading from current article
}
});
n++;
});
});
}
Is there a more elegant way of doing this ?