From 142ae594d472e4acce3f404dbbc7df86ad0f833f Mon Sep 17 00:00:00 2001 From: Ian Lai Date: Sat, 17 Jan 2015 02:31:54 +0000 Subject: [PATCH] Reworked internal API design; and introduced cycle() (similar to start), stop() and complete callback setting (called after the timer to start a new cycle) --- dist/morphist.js | 64 +++++++++++++++++++++++--------------------- dist/morphist.min.js | 2 +- 2 files changed, 34 insertions(+), 32 deletions(-) diff --git a/dist/morphist.js b/dist/morphist.js index 0ed6ea3..e532c70 100644 --- a/dist/morphist.js +++ b/dist/morphist.js @@ -5,7 +5,7 @@ * Built on jQuery Boilerplate * http://jqueryboilerplate.com/ * - * Copyright 2014 Ian Lai and other contributors + * Copyright 2015 Ian Lai and other contributors * Released under the MIT license * http://ian.mit-license.org/ */ @@ -21,7 +21,8 @@ defaults = { animateIn: "bounceIn", animateOut: "rollOut", - speed: 2000 + speed: 2000, + complete: $.noop }; function Plugin (element, options) { @@ -29,48 +30,49 @@ this.settings = $.extend({}, defaults, options); this._defaults = defaults; - this._name = pluginName; - this.init(); + this._init(); } Plugin.prototype = { - init: function () { + _init: function () { this.children = this.element.children(); this.element.addClass("morphist"); - this.index = -1; - this.cycle(); - }, - animate: function () { - var $that = this; - - ++this.index; - this.prev = this.index; - this.children.eq(this.index).addClass("animated " + this.settings.animateIn); - - setTimeout(function () { - $that.cycle(); - }, this.settings.speed); + this.index = 0; + this.cycle(); }, cycle: function () { var $that = this; + this._animateIn(); - if ((this.index + 1) === this.children.length) { - this.index = -1; - } - - if (typeof this.prev !== "undefined" && this.prev !== null) { - this.children.eq(this.prev) - .removeClass(this.settings.animateIn) - .addClass(this.settings.animateOut) - .one("webkitAnimationEnd mozAnimationEnd MSAnimationEnd oanimationend animationend", function() { + this._timeout = setTimeout(function () { + $that._animateOut() + .one("webkitAnimationEnd mozAnimationEnd MSAnimationEnd" + + "oanimationend animationend", function () { $(this).removeClass(); - $that.animate(); + if ($that.index + 1 === $that.children.length) { + $that.index = -1; + } + ++$that.index; + $that.cycle(); }); - return; - } + }, this.settings.speed); - this.animate(); + if ($.isFunction(this.settings.complete)) { + this.settings.complete.call(this); + } + }, + stop: function () { + clearTimeout(this._timeout); + }, + _animateIn: function () { + return this.children.eq(this.index) + .addClass("animated " + this.settings.animateIn); + }, + _animateOut: function () { + return this.children.eq(this.index) + .removeClass() + .addClass("animated " + this.settings.animateOut); } }; diff --git a/dist/morphist.min.js b/dist/morphist.min.js index 497dc1a..eafd5f7 100644 --- a/dist/morphist.min.js +++ b/dist/morphist.min.js @@ -1 +1 @@ -/*! Morphist - v1.1.2 - 2014-10-14 */!function(a){"use strict";function b(b,e){this.element=a(b),this.settings=a.extend({},d,e),this._defaults=d,this._name=c,this.init()}var c="Morphist",d={animateIn:"bounceIn",animateOut:"rollOut",speed:2e3};b.prototype={init:function(){this.children=this.element.children(),this.element.addClass("morphist"),this.index=-1,this.cycle()},animate:function(){var a=this;++this.index,this.prev=this.index,this.children.eq(this.index).addClass("animated "+this.settings.animateIn),setTimeout(function(){a.cycle()},this.settings.speed)},cycle:function(){var b=this;return this.index+1===this.children.length&&(this.index=-1),"undefined"!=typeof this.prev&&null!==this.prev?void this.children.eq(this.prev).removeClass(this.settings.animateIn).addClass(this.settings.animateOut).one("webkitAnimationEnd mozAnimationEnd MSAnimationEnd oanimationend animationend",function(){a(this).removeClass(),b.animate()}):void this.animate()}},a.fn[c]=function(d){return this.each(function(){a.data(this,"plugin_"+c)||a.data(this,"plugin_"+c,new b(this,d))})}}(jQuery); \ No newline at end of file +/*! Morphist - v2.0.0 - 2015-01-17 */!function(a){"use strict";function b(b,c){this.element=a(b),this.settings=a.extend({},d,c),this._defaults=d,this._init()}var c="Morphist",d={animateIn:"bounceIn",animateOut:"rollOut",speed:2e3,complete:a.noop};b.prototype={_init:function(){this.children=this.element.children(),this.element.addClass("morphist"),this.index=0,this.cycle()},cycle:function(){var b=this;this._animateIn(),this._timeout=setTimeout(function(){b._animateOut().one("webkitAnimationEnd mozAnimationEnd MSAnimationEndoanimationend animationend",function(){a(this).removeClass(),b.index+1===b.children.length&&(b.index=-1),++b.index,b.cycle()})},this.settings.speed),a.isFunction(this.settings.complete)&&this.settings.complete.call(this)},stop:function(){clearTimeout(this._timeout)},_animateIn:function(){return this.children.eq(this.index).addClass("animated "+this.settings.animateIn)},_animateOut:function(){return this.children.eq(this.index).removeClass().addClass("animated "+this.settings.animateOut)}},a.fn[c]=function(d){return this.each(function(){a.data(this,"plugin_"+c)||a.data(this,"plugin_"+c,new b(this,d))})}}(jQuery); \ No newline at end of file