What would be the best way to achieve a flip over effect using AngularJS animations?
I would like the flip over effect to occur on click. Every time it's clicked, it should flip over to the other side.
Ideally, I guess, I'm looking for a directive implementation that uses Angular animations.
A custom directive is the way to go, one that can be used as:
I think it should have certain properties:
Programatically controlled by an attribute. In this case, a string that is equal to 'front' or 'back'
this would allow programmatic access via the surrounding scope.
Integrated with
ngAnimate
/$animate
. Specifically, ifngAnimate
is removed or disabled, the animation should not occur, but the reveal of the other side happen immediately. Using$animate.addClass
/$animate.removeClass
would achieve this, adding/removing aflip-visible
class together withdisplay:block
anddisplay:none
styles to make sure the right side is visible/hidden when the animations are disabled.Controlled by CSS, with defaults. So if you want to change the duration of the flip, it's a CSS, and not a Javascript, addition.
So it will have a style sheet to add styles required for the various stages of
$animate.addClass
/$animate.removeClass
CSS animations explained at Year of Moo and $animate docs . The class will beflip-visible
, so the extra classes will be.flip-visible-add
,.flip-visible-add-active
,.flip-visible-remove
, and.flip-visible-remove-active
classes.The full set of styles can be seen at http://plnkr.co/edit/bbYbMhiURnm6FqC9patp?p=preview, but the main construction is of the form:
Putting all this together, the directive is quite short:
This can all be seen in an example, together with the stylesheets to make it all work, at http://plnkr.co/edit/bbYbMhiURnm6FqC9patp?p=preview
I would simply add / remove a class on click.
If you want to hook into the angular animation system then take a look at the $animate service, in particular
add/remove/setClass()
. The service is usually used in directives. You might want to create a directive that reacts on a click event and triggers the animation. You even get informed when the animation has completed.Chances are that it's not worth it ;)