I am trying to use the new ng-animate directive, and am struggling to get the desired effect when used with a ng-repeat. I am trying to make items grow when entering, and shrink when leaving. So far the enter is working, but the shrink animation fails.
I have set up a fiddle here so you can see my issue:-
http://jsfiddle.net/rpk98c/6t42M/1/
The relevant HTML is:-
<ul>
<li ng-animate="{enter: 'repeat-enter',
leave: 'repeat-leave',
move: 'repeat-move'}" ng-click="remove($index)" ng-repeat="name in names">{{name}}</li>
</ul>
And the relevant CSS:-
.repeat-enter-setup, .repeat-leave-setup, .repeat-move-setup {
-webkit-transition:all linear 1s;
-moz-transition:all linear 1s;
-ms-transition:all linear 1s;
-o-transition:all linear 1s;
transition:all linear 1s;
}
.repeat-enter-setup {
max-height: 0;
opacity:0;
}
.repeat-enter-setup.repeat-enter-start {
max-height: 250px;
opacity:1;
}
.repeat-leave-setup {
opacity:1;
max-height: 250px;
}
.repeat-leave-setup.repeat-leave-start {
opacity:0;
max-height: 0;
}
Anyone know where I've gone wrong?
Thanks,
Ryan
ps just noticed in IE 10 no animations work! I'm testing in Chrome for now
You need to switch the first line of the css to affect the "start" classes instead of the setup ones. I know, that's not how they do the examples but it will work. So make it:
.repeat-enter-start, .repeat-leave-start, .repeat-move-start {
-webkit-transition:all linear 1s;
-moz-transition:all linear 1s;
-ms-transition:all linear 1s;
-o-transition:all linear 1s;
transition:all linear 1s;
}
This helps because you are trying to animate a property that the element does not usually possess. Elements will not usually have the max-height property set at all. So when you apply a max-height at the same time as the transition it will animate the fact that you added the property! This is before the "start" class is even applied which means that the setup stage will not be finished (since the animation takes 1 second) before the start stage is supposed to begin. So when you apply the "leave" animation the following steps will happen.
But if we move the transition property from the "setup"-step to the "start"-step, like in the css above, step 3 will be instant since it has no transition. The transition appears first at step 4. So we have:
You don't have this problem with opacity since you are animating from the default value of 1, there is no animation to get in the way in step 3. It also seems that enter events are handled a bit differently since they are more likely to animate from a non-default value, so you will not see this behavior on ever events.
Edit:
I experimented a bit more with this and it turns out my answer does not work for Firefox (at least not on FF Windows). First of all FF seems to just straight up fail if you animate a non existing property, so you need to make sure that your elements have a max-height from the start, something like:
li {
max-height: 250px;
}
This fixes leave for me, but for some reason no enter animation at all will work in Firefox on windows. Still trying to figure that one out.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With