Created
March 11, 2014 10:26
-
-
Save jackgrant87/9483103 to your computer and use it in GitHub Desktop.
AngularJS directive to fire a CSS animation once on click
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // 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