Last active
August 29, 2015 13:56
-
-
Save nwwells/8874532 to your computer and use it in GitHub Desktop.
Animate
This file contains hidden or 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
// TODO - remove dependencies on jquery, underscore, require, and backbone. | |
define(function (require) { | |
'use strict'; | |
var _ = require('underscore'); | |
var Backbone = require('backbone'); | |
var transitionEnd = [ | |
'transitionend', | |
'webkitTransitionEnd', | |
'oTransitionEnd', | |
'MSTransitionEnd' | |
].join(' '); | |
/*=================== | |
| Animation Helpers | | |
===================*/ | |
var executeOrApply = function (functionOrStyles, $el) { | |
if ('function' === typeof functionOrStyles) { | |
functionOrStyles($el); | |
} else { | |
$el.css(functionOrStyles); | |
} | |
}; | |
return function animateStyleChange($el, startState, endState) { | |
var animationEvents = _.extend({}, Backbone.Events); | |
if (_.isEqual(startState, endState)) { | |
_.defer(function () { | |
animationEvents.trigger('end', $el); | |
}); | |
return animationEvents; | |
} | |
// need to apply start state to ensure a smooth transition | |
executeOrApply(startState, $el); | |
_.defer(function () { | |
//after that's fully rendered, start animating | |
$el.addClass('is-animating'); | |
executeOrApply(endState, $el); | |
animationEvents.trigger('start', $el); | |
var transitionEndHandler = function () { | |
$el.removeClass('is-animating'); | |
animationEvents.trigger('end', $el); | |
_.defer(function () { | |
animationEvents.trigger('after', $el); | |
}); | |
}; | |
$el.one(transitionEnd, transitionEndHandler); | |
}); | |
return animationEvents; | |
}; | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment