Skip to content

Instantly share code, notes, and snippets.

@leehsueh
Created October 26, 2012 01:47
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save leehsueh/3956497 to your computer and use it in GitHub Desktop.
Save leehsueh/3956497 to your computer and use it in GitHub Desktop.
Angular directive for jquery fade/slide animations. An approximate drop-in substitution for ng-show. Options can be passed using jq-options and specifying an object.
var app = angular.module('myApp', []);
app.directive('jqShowEffect', ['$timeout', function($timeout) {
return {
restrict: 'A',
link: function(scope, element, attrs) {
// configure options
var passedOptions = scope.$eval(attrs.jqOptions);
// defaults
var options = {
type: 'fade', // or 'slide'
duration: 200,
delay: 0,
hideImmediately: false, // if true, will hide without effects or duration
callback: null
};
$.extend(options, passedOptions);
var type = options.type;
var duration = options.duration;
var callback = options.callback;
var delay = options.delay;
var hideImmediately = options.hideImmediately;
// watch the trigger
var jqElm = $(element);
scope.$watch(attrs.jqShowEffect, function(value) {
if (hideImmediately && !value) {
jqElm.hide(0, callback);
} else {
$timeout(function() {
if (type == 'fade') {
value ? jqElm.fadeIn(duration, callback) : jqElm.fadeOut(duration, callback);
} else if (type == 'slide') {
value ? jqElm.slideDown(duration, callback) : jqElm.slideUp(duration, callback);
} else {
value ? jqElm.show(duration, callback) : jqElm.hide(duration, callback);
}
}, delay);
}
});
}
}
}]);
<div jq-show-effect="whatever you use for ng-show" jq-options="{type:'fade', duration:200, delay:200, hideImmediately: true}">Content</div>
@mleanos
Copy link

mleanos commented Mar 10, 2016

This is beautiful! Thank you!

I extended this a little bit by adding separate options for incoming (slideIn, fadeIn) & outgoing (slideOut, fadeOut).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment