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.
I realise there is a benefit to not integrating with the
$animate
service, and having a purely class-based solution.If you use
$animate
withaddClass
andremoveClass
, but interrupt the animation (say, by clicking quickly and repeatedly on the element), the animation will 'jerk' to its end/starting point, and then animate from that position, at least on Chrome. Using a pure CSS solutions avoids this issue, and always animates from the exact current point, giving a smoother effect.An added benefit is the solution is also simpler, and you don't need a custom directive.
For example, the HTML can be as follows:
I use custom elements, but they don't need to have any directives attached: they are just for CSS to hook into:
This can be seen in a demo at http://plnkr.co/edit/A7IeGa1JEsZishmTDTaK?p=preview
You can use
ng-click
andng-class
to add a class when the flip container is clicked.This is essentially the angular way of doing what Walsh suggested in his article:
The only change to David Walsh's css was removing the
:hover
selector - the html structure is unchanged. It works nicely in chrome and firefox.. but the flip isn't as pretty in IE.Here is a working demo: http://plnkr.co/edit/0dn775vpuoOeh2PS1T6k?p=preview
Update
I created a simple directive to encapsulate this basic technique. It allows you to flip over a black card, to reveal a picture on the other side.
Here is a link to a new demo: http://plnkr.co/X4pSav
You are going to want to create 3 divs.
You then position back behind front using z-index, and flip it upside down using rotateX (-180deg or so). Set a transition on wrapper as well.
Then, on click of wrapper, rotateX(+180deg). This will pretty much infinitely flip it over.
** Update: For angular, bind to click and use setClass to toggle between two classes on wrapper, one at rotateX(0deg) , the other at rotateX(180deg)
Here is a slightly modified version of artur's answer:
DEMO
Main differences:
PLNKR - here is a seed of a configurable angular directive that provides 3d flipping functionality. I do not see any good reason why to use
ngAnimate
for it.basic usage
Comments
directive
all names should be configurable.flip-width
andflip-height
sets style offlip
and bothflip-panels
.front
andback
are set.flip-panel
isfront
and the second isback
.Due to usage of(you are right Misha no transclusion needed)transclusion
content of theflip-panel
may be arbitrary html.-webkit
. (update to make it work in Firefox, just duplicate all pieces with-webkit
with no prefix - you do not need-moz
)UPDATE
PLNKR - here is an updated and extended version. It shows what I meant by making the directive configurable. In more details:
flipConfig
viaprovider
, that allows to set inapp.config
:flip-show
attribute that specifies which side to show.flip-show
we can trigger the flip action from outside of the directive.(btw: it is just a seed and it may be improved in a lot of ways. E.g: specifying axis, specifying origin of the transform, specifying radius and margin of the panels, allowing flip on hover, defaults colors, margins and so on)
I had the same usecase just recently for an angular memory game.
My implementation is the same by the idea of the other answers. I also released the flipping code along with a DEMO.
Github: https://github.com/zwacky/angular-flippy
P.s.: Looks i'm late to the party ;)