Skip to content

Commit

Permalink
Reworked internal API design; and introduced cycle() (similar to star…
Browse files Browse the repository at this point in the history
…t), stop() and complete callback setting (called after the timer to start a new cycle)
  • Loading branch information
MrSaints committed Jan 17, 2015
1 parent 63b9e89 commit 142ae59
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 32 deletions.
64 changes: 33 additions & 31 deletions dist/morphist.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* Built on jQuery Boilerplate
* http:https://jqueryboilerplate.com/
*
* Copyright 2014 Ian Lai and other contributors
* Copyright 2015 Ian Lai and other contributors
* Released under the MIT license
* http:https://ian.mit-license.org/
*/
Expand All @@ -21,56 +21,58 @@
defaults = {
animateIn: "bounceIn",
animateOut: "rollOut",
speed: 2000
speed: 2000,
complete: $.noop
};

function Plugin (element, options) {
this.element = $(element);

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);
}
};

Expand Down
2 changes: 1 addition & 1 deletion dist/morphist.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 142ae59

Please sign in to comment.