Skip to content

Instantly share code, notes, and snippets.

@anonymoussc
Created August 8, 2015 23:12
Show Gist options
  • Save anonymoussc/6dac76558acaa4a790e6 to your computer and use it in GitHub Desktop.
Save anonymoussc/6dac76558acaa4a790e6 to your computer and use it in GitHub Desktop.
AngularJS custom directive staggering animation

##AngularJS custom directive staggering animation

<!DOCTYPE html>
<html ng-app="myApp">
<head>
<title>AngularJS custom directive staggering animation</title>
<link href="style.css" rel="stylesheet"/>
</head>
<body>
<div add-directive="">
<div>Click here!</div>
</div>
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.3.0/angular.min.js"></script>
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.3.0/angular-animate.min.js"></script>
<script src="script.js"></script>
</body>
</html>
function addDirectiveFn($animate) {
return {
link : function ($scope, $element, $attrs) {
var firstElement = angular.element('<div class="addAnimation">1</div>');
var secondElement = angular.element('<div class="addAnimation">2</div>');
var thirdElement = angular.element('<div class="addAnimation">3</div>');
$element.on('click', function () {
$animate.enter(firstElement, $element);
$animate.enter(secondElement, $element);
$animate.enter(thirdElement, $element);
//Trigger digest in this case, because this listener function is out of the angular world
$scope.$apply();
});
}
}
}
angular.module('myApp', ['ngAnimate'])
.directive('addDirective', addDirectiveFn);
/* add custom directive animation */
.addAnimation.ng-enter {
-webkit-transition : opacity ease-in-out 1s;
-moz-transition : opacity ease-in-out 1s;
-ms-transition : opacity ease-in-out 1s;
-o-transition : opacity ease-in-out 1s;
transition : opacity ease-in-out 1s;
}
.addAnimation.ng-enter {
opacity : 0;
}
.addAnimation.ng-enter-stagger {
-webkit-transition-delay : 0.2s;
transition-delay : 0.2s;
-webkit-transition-duration : 0;
transition-duration : 0;
}
.addAnimation.ng-enter.ng-enter-active {
opacity : 1;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment