Skip to content

Instantly share code, notes, and snippets.

@anonymoussc
Created August 7, 2015 10:30
Show Gist options
  • Save anonymoussc/a7d98ac791db8c69a3cc to your computer and use it in GitHub Desktop.
Save anonymoussc/a7d98ac791db8c69a3cc to your computer and use it in GitHub Desktop.
AngularJS $animate.enter and $animate.leave

##AngularJS $animate.enter and $animate.leave

<!DOCTYPE html>
<html ng-app="myApp">
<head>
<title>AngularJS $animate.enter and $animate.leave</title>
<link href="style.css" rel="stylesheet"/>
</head>
<body>
<div toggle-directive="" class="enterAnimation"></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>
var app = angular.module('myApp', ['ngAnimate'])
.directive('toggleDirective', function ($animate) {
return {
link : function ($scope, $element, $attrs) {
var firstElement = angular.element('<div class="toggleAnimation">First element! Click here to trigger $animate.leave for this element and $animate.enter for the second element</div>');
var secondElement = angular.element('<div class="toggleAnimation">Second element! Click here to trigger $animate.leave for this element and $animate.enter for the first element</div>');
//Adds the firstElement
$animate.enter(firstElement, $element);
var isActive = false;
$element.on('click', function () {
isActive = !isActive;
// Toggle between firstElement and secondElement
if (isActive) {
$animate.leave(firstElement);
$animate.enter(secondElement, $element);
} else {
$animate.leave(secondElement);
$animate.enter(firstElement, $element);
}
//Trigger digest in this case, because this listener function is out of the angular world
$scope.$apply();
});
}
}
});
/* toggle animation */
.toggleAnimation.ng-enter,
.toggleAnimation.ng-leave {
-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;
}
.toggleAnimation.ng-enter,
.toggleAnimation.ng-leave.ng-leave-active {
opacity : 0;
}
.toggleAnimation.ng-leave,
.toggleAnimation.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