Skip to content

Instantly share code, notes, and snippets.

@cfenzo
Created August 25, 2014 11:17
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cfenzo/63576935f3b549280f1b to your computer and use it in GitHub Desktop.
Save cfenzo/63576935f3b549280f1b to your computer and use it in GitHub Desktop.
A tiny ractive transitions plugin for animate.css-animations.
/*
Requires animate.css, and support for the 'transitionend' event.
(See https://developer.mozilla.org/en-US/docs/Web/Events/transitionend for browser support)
*/
(function ( global, factory ) {
'use strict';
// Common JS (i.e. browserify) environment
if ( typeof module !== 'undefined' && module.exports && typeof require === 'function' ) {
factory( require( 'ractive' ) );
}
// AMD?
else if ( typeof define === 'function' && define.amd ) {
define([ 'ractive' ], factory );
}
// browser global
else if ( global.Ractive ) {
factory( global.Ractive );
}
else {
throw new Error( 'Could not find Ractive! It must be loaded before the ractive.transitions.animatecss plugin' );
}
}( typeof window !== 'undefined' ? window : this, function ( Ractive ) {
'use strict';
var ANIMATION_END_EVENT = (function(){
var eventName,
ANIMATION_END_EVENT_NAMES = {
'animation': 'animationend',
'-o-animation': 'oAnimationEnd',
'-moz-animation': 'animationend',
'-webkit-animation': 'webkitAnimationEnd',
'-ms-animation': 'animationend'
},
fakeEl = document.createElement('fakeelement');
for (eventName in ANIMATION_END_EVENT_NAMES) {
if (typeof(fakeEl.style[eventName]) !== 'undefined') {
return ANIMATION_END_EVENT_NAMES[eventName];
}
}
return null;
})();
var animateCSS = function ( t, transition ) {
// Process parameters. No error checking for non-existing transitions(!!)
transition = transition || (t.isIntro ? Ractive.transitions.animatecss.defaultIntro : Ractive.transitions.animatecss.defaultOutro);
// add 'animated' class if not present
if(!t.node.classList.contains('animated')) t.node.classList.add('animated');
// add the transition class, the transitions starts immediately
t.node.classList.add(transition);
// After the animation has ended, call `t.complete()` to signal that we've finished
t.node.addEventListener(ANIMATION_END_EVENT,function(){
t.node.classList.remove(transition);
t.complete();
});
};
Ractive.transitions.animatecss = animateCSS;
Ractive.transitions.animatecss.defaultIntro = 'slideInDown';
Ractive.transitions.animatecss.defaultOutro = 'slideOutUp';
}));
@cncolder
Copy link

After the animation has ended, you'd better remove event listener.

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