I am using $().promise().then() to start animation after image source is loaded. Everything works, but I am not sure... Is this The Right Way? Is there another jQuery construction to run animation after all images are loaded?
WARNING
$(window).load() will not work. I need solution - callback, which will be fired AFTER ALL images loaded. But after all PROGRAMATICALLY loaded images - see
prop("src","http://lorempixel.com/320/180/business").
$(function() {
var options = {
step: function(now) {
$(this).css("filter", $(this).prop("id")+"("+now+"%)")
},
duration: 2000,
easing: "linear"
},
animate = function() {
$(this).animate({filter: "100%"}, options);
}
$("img[id]").each(function() {
$(this)
.prop("src", "http://lorempixel.com/320/180/business")
.promise()
.then(animate);
})
})
:root {
counter-reset: cntr;
/* CSS variables - MS from Edge 15 */
--h: 256;
--s: 50%;
--l: 100%;
}
ul {
color: hsl(var(--h), var(--s), var(--l));
}
li:after {
counter-increment: cntr;
content: attr(data-text)" "counter(cntr);
}
figure {
float: left;
margin-bottom: 10px;
}
/* Filters - disabled. Done by animation
#sepia {
filter: sepia(100%);
}
#grayscale {
filter: grayscale(100%);
}
#invert {
filter: invert(100%);
} */
<!DOCTYPE html>
<meta charset="utf-8">
<title>New CSS features and "advanced" jQuery animation</title>
<ul>
<li data-text='Item'>
<li data-text='Item'>
<li data-text='Item'>
<li data-text='Item'>
<li data-text='Item'>
<li data-text='Item'>
</ul>
<figure>
<figcaption>Sepia</figcaption>
<img id="sepia" src="#">
</figure>
<figure>
<figcaption>Grayscale</figcaption>
<img id="grayscale" src="#">
</figure>
<figure>
<figcaption>Inverted</figcaption>
<img id="invert" src="#">
</figure>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>