Skip to content

Instantly share code, notes, and snippets.

@vermilion1
Created December 11, 2012 09:00
Show Gist options
  • Save vermilion1/4257182 to your computer and use it in GitHub Desktop.
Save vermilion1/4257182 to your computer and use it in GitHub Desktop.
CSS animation plugin
/*!
* CSS animation plugin
* Copyright 2012, Vitaliy Petrychuk
*/
;(function ($, _) {
// usage
// 1! register ($el - jQuery element)
// 2. setAnimationClass || onAnimationStart || onAnimationEnd
var Animation;
var animationEndEvents = ['animationend'];
var prefix;
$.browser.webkit && (prefix = 'webkit');
$.browser.mozilla && (prefix = 'Moz');
$.browser.msie && (prefix = 'ms');
prefix && animationEndEvents.push(prefix + 'AnimationEnd');
animationEndEvents = animationEndEvents.join(' ');
Animation = {
_resetData : function() {
this.$el && this._className && this.$el.removeClass(this._className);
this.$el = null;
this._className = null;
this._onAnimationStart = function() {};
this._onAnimationEnd = function() {};
},
register : function($el) {
this._resetData();
this.$el = $el;
return this;
},
setAnimationClass : function(className) {
this._className = className;
return this;
},
onAnimationStart : function(callback) {
_.isFunction(callback) && (this._onAnimationStart = callback);
return this;
},
onAnimationEnd : function(callback) {
_.isFunction(callback) && (this._onAnimationEnd = callback);
return this;
},
start : function() {
if (!this.$el || !this._className) return false;
this._onAnimationStart();
this.$el
.addClass(this._className)
.on(animationEndEvents, this._onAnimationEnd);
return this;
}
};
if (typeof define === 'function' && define.amd) {
define('css-animation', [], function () {
return Animation;
});
}
}(jQuery, _));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment