Skip to content

Instantly share code, notes, and snippets.

@culttm
Created August 9, 2016 14:08
Show Gist options
  • Select an option

  • Save culttm/ef0dd3a01ab603e4d84d01885ea6d382 to your computer and use it in GitHub Desktop.

Select an option

Save culttm/ef0dd3a01ab603e4d84d01885ea6d382 to your computer and use it in GitHub Desktop.
(function ($) {
var AnimatedEl = (function(){
var AnimatedEl = function (o) {
this.el = $(o.el);
this.animationType = $(o.el).data('action');
this.checkInterval = o.checkInterval;
this.timeout = null;
this.init();
};
AnimatedEl.prototype.init = function () {
this.setupListeners();
};
AnimatedEl.prototype.setupListeners = function () {
var self = this;
self.el.on('mouseenter', function () {
self.run($(this));
}).on('mouseleave', function () {
clearInterval(self.timeout)
});
};
AnimatedEl.prototype.run = function (el) {
var self = this;
this.timeout = setInterval(function () {
el.addClass(self.animationType);
}, self.checkInterval);
el.on("webkitAnimationEnd mozAnimationEnd oAnimationEnd animationEnd", function(e){
self.stop(el);
});
};
AnimatedEl.prototype.stop = function (el) {
el.removeClass(this.animationType);
};
return AnimatedEl;
})();
$.each($('.js-anim-el'), function (i, el) {
new AnimatedEl({
el: el,
checkInterval: 200
});
})
})(jQuery);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment