Skip to content

Instantly share code, notes, and snippets.

@jackgrant87
Created March 11, 2014 10:26
Show Gist options
  • Select an option

  • Save jackgrant87/9483103 to your computer and use it in GitHub Desktop.

Select an option

Save jackgrant87/9483103 to your computer and use it in GitHub Desktop.
AngularJS directive to fire a CSS animation once on click
// General purpose 'animate' directive.
//
// Takes these arguments:
// animate-once-target="{css selector}" animate-once-animation="{name of css animation}" animate-once-on="{event type}"
//
// Requires CSS set up as here: http://codepen.io/jackshepherd/pen/vkFad
App.directive('animateOnce', function() {
return {
restrict: 'A',
link: function(scope, element, attr) {
var $target = (attr.animateOnceTarget) ? $(attr.animateOnceTarget) : element,
eventType = (attr.animateOnceOn) ? attr.animateOnceOn : 'click';
if(!attr.animateOnceAnimation) {
throw new typeerror('Animate directive requires animation name');
}
$target.on('webkitAnimationEnd', function() {
$(this).css('webkitAnimationName', '');
})
element.on(eventType, function() {
$target.css('webkitAnimationName', attr.animateOnceAnimation)
});
}
};
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment