The experience I'm trying to create is one where a background image is first loaded, then an animation is triggered to fade in the element it is attached to. I am doing this in AngularJS using ngAnimate and waitForImages. Specifically, I have the following view in my <body>
:
<div ng-view="" ng-class="pageClass">
<br>
<br>
<br>
<h1 id="loading-text">Loading...</h1>
</div>
Where pageClass
is set to landing-page
by $routingProvider
and the following controller and animation combination are supposed to give me the desired result:
myModule.controller('LandingPageCtrl', ['$timeout', '$animate', function ($timeout, $animate) {
$animate.on('enter', angular.element('.ng-scope'), function (element) {
console.log('cool it fired!');
});
}]).animation('.landing-page', ['$animateCss', function ($animateCss) {
return {
enter: function(element, doneFn) {
console.log('waiting...');
element.waitForImages(true).done(function () {
console.log('loaded the image!');
return $animateCss(element, {
event: 'enter',
structural: true
});
});
}
};
}]);
And here are my SASS classes (scss):
.landing-page {
&.ng-enter {
transition: opacity 1s;
-webkit-transition: opacity 1s;
}
&.ng-leave {
transition: opacity 3s, left 1s ease;
-webkit-transition: opacity 3s, left 1s ease;
}
&.ng-enter,
&.reverse.ng-leave-active {
opacity: 1;
left: 0;
}
&.ng-enter-active,
&.ng-leave,
&.reverse.ng-enter-active,
&.reverse.ng-leave {
opacity: 0;
left: 0;
}
&.ng-leave-active,
&.reverse.ng-enter {
opacity: 0;
left: -100%;
}
}
The behavior I am experiencing is that after the Loading...
text disappears, I get waiting...
in the console with simultaneous showing of the element with the background image not loaded completed, then cool it fired!
. I have scoured the $animate and $animateCss docs for clues and it looks to me that I am using them correctly and they are just not working as described. If $animate.on('enter',...)
is supposed to fire after the enter animation, why is it firing before the loaded the image!
console log? Perhaps I'm missing something obvious since I have been looking at this chunk of code for too long...