I'm trying to write a directive to move an element from one position on screen to another on click, using a CSS transition, where the position is determined by Javascript / attribute on the element, and not known ahead of time in the pre-written CSS. Also, I need a callback, to a function in the $scope, done at the end of the transition, which is specified in a custom attribute. So the element looks like
<button move-me="100" move-me-after="afterMove()">Move Me!</button>
In order to use $animate for this, in my custom directive moveMe, on click I dynamically add a style block to the page
<style> .move-me-1 {....}</style>
and then use
$animate.addClass(iElement, 'move-me-1', function() { ... });
to handle the animation and the callback once it's finished. An example plunkr is available at
http://plnkr.co/edit/XRMRO93vl248Ve02Jnpo?p=preview
This seems overly complicated: adding style tags to the page seems quite convoluted. I'm doing this so the transition itself can be specified in an external style sheet, in terms of the run time, and the timing function, and using $animate.addClass so Angular can then determine the correct time to fire the callback.
Is there a simpler/better way of achieving what I want?
This example is simplified to my actual use case. In my case the element animated is created in the directive, and removed at the end, and is purely a temporary visual aid. In addition to position, height + width are also dynamically determined for both the start and final style of the element, using a CSS transition between the states. Each of the styles can have a different transition time + timing function to achieve the effect I'm after. Multiple elements can be moved to / from different positions at the same time, so the JS/CSS must allow that.