##AngularJS $animate.enter and $animate.leave
Created
August 7, 2015 10:30
-
-
Save anonymoussc/a7d98ac791db8c69a3cc to your computer and use it in GitHub Desktop.
AngularJS $animate.enter and $animate.leave
This file contains 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
<!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> |
This file contains 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
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(); | |
}); | |
} | |
} | |
}); |
This file contains 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
/* 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