var app = angular.module('myApp', []);
app.directive('startAnimate', function () {
return {
scope: {
startAnimate: '@', //animation name (string)
seconds: '@', //duration in seconds (int)
startOnCreation: '=?', //start on creation of element (boolean) - optional
onAnimationEnd: '=?', //callback at end - optional
workspace: '=?' //create an object to allow two-way data binding with primitive values (boolean, int, ...) - optional
},
link: function (scope, element, attributes) {
var animation = scope.startAnimate + ' ' + scope.seconds + 's';
if (!scope.workspace) {
scope.workspace = {};
}
scope.workspace.running = false;
var setElementSAnimation = function (animation) {
element.css({
WebkitAnimation: animation,
animation: animation
});
}
var startAnimation = function () {
setElementSAnimation(animation);
scope.workspace.running = true;
}
if (scope.startOnCreation) {
startAnimation();
}
element.click(function () {
scope.$apply(function () {
startAnimation();
});
});
if (!scope.onAnimationEnd) {
scope.onAnimationEnd = function () { };
}
var oldAnimationEnd = scope.onAnimationEnd;
scope.onAnimationEnd = function () {
oldAnimationEnd();
//clean animation to allow to repeat it
setElementSAnimation('');
scope.$apply(function () {
scope.workspace.running = false;
})
};
element.on("webkitAnimationEnd animationend", scope.onAnimationEnd);
}
}
});
app.controller('myCtrl', function ($scope) {
$scope.myfunc = function () {
alert('finish');
}
$scope.showElement = false;
$scope.workspace = {
running: false
}
});
#myDIV {
width: 100px;
height: 100px;
background: orange;
position: relative;
}
@-webkit-keyframes mymove {
from {
top: 0px;
left: 0px;
}
to {
top: 80px;
left: 160px;
}
}
@keyframes mymove {
from {
top: 0px;
left: 0px;
}
to {
top: 80px;
left: 160px;
}
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div data-ng-app="myApp" data-ng-controller="myCtrl">
<div data-ng-if="workspace.running">
Yes, it's running!
</div>
<div id="myDIV" data-ng-if="showElement" data-start-animate="mymove" data-start-on-creation="true" data-seconds="4" data-on-animation-end="myfunc" data-workspace="workspace">
<span>Click me to start the animation.</span>
</div>
<button type="button" data-ng-click="showElement = !showElement">Toggle element</button>
</div>