I am trying to use jQuery animation with angular's ngAnimate module. In particular, for this example, jQuery's fadeIn and fadeOut. I can get this to work with css transitions, using .ng-enter and .ng-leave etc, but for some reason, not jQuery.
The fade in for the following angular code does not work. The element just shows up straight away.
var app = angular.module('app', ['ngAnimate']);
app.controller('Ctrl', ['$scope',
function($scope) {
$scope.data = {};
$scope.data.toggle = true;
}
]);
app.animation('.toggle', function() {
return {
enter: function(element, done) {
element.fadeIn(1000, done);
},
leave: function(element, done) {
element.fadeOut(1000, done);
}
};
});
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.5/angular.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.4.0/angular-animate.min.js"></script>
<div ng-app="app" ng-controller="Ctrl">
<div class="btn btn-primary" ng-click="data.toggle = ! data.toggle">
Toggle Button
</div>
<div class="panel panel-default toggle" ng-if="data.toggle">
<div class="panel-body">
Content being toggled
</div>
</div>
</div>
However, when using the same fade functions with just jQuery, the animation works fine:
$(function() {
var container = $('.container');
var btn = $('.btn');
var panel = $('.panel');
var animating = false;
btn.on('click', function() {
if (animating)
return;
if (panel.is(':visible')) {
animating = true;
panel.fadeOut(1000, function() {
animating = false;
panel.remove();
});
return;
}
animating = true;
panel.appendTo(container).fadeIn(1000, function() {
animating = false
});
});
});
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div class="container">
<div class="btn btn-primary">
Toggle Button
</div>
<div class="panel panel-default toggle">
<div class="panel-body">
Content being toggled
</div>
</div>
</div>
Note, in both cases the panel element is being removed/ added to the DOM. I'm doing this in jQuery to mimic ng-if behaviour.
Is there a way to use jQuery animiations when angular adds an element to the DOM via ng-if? If it's possible with ng-show that would be even better. I don't need workarounds not using jQuery, I can figure those out myself. The answer must use jQuery animations. A small bit of css to support the jQuery animation is fine, however.
slideUpslideDown,Blind,Bounce, or indeed any effect from jqueryui.com/effect without having to create the code for those off the shelf effects.