(function($) { var Flapper = function($ele, options) { var _this = this; this.id = Math.floor(Math.random() * 1000) + 1; this.$ele = $ele; this.options = $.extend({}, this.defaults, options); // is transform loaded? this.options.transform = this.options.transform && $.transform; this.$div = $('
'); this.$div.attr('class', 'flapper ' + this.$ele.attr('class')); this.$ele.hide().after(this.$div); this.$ele.bind('change.flapper', function(){ _this.update(); }); this.init(); } Flapper.prototype = { defaults: { width: 6, format: null, align: 'right', padding: ' ', chars: null, chars_preset: 'num', timing: 250, min_timing: 10, threshhold: 100, transform: true, on_anim_start: null, on_anim_end: null, cycleInterval: null, }, init: function() { var _this = this; this.digits = []; for (i=0; i this.options.width) { var overage = digits.length - this.options.width; if (this.options.align == 'left') { digits.splice(-1, overage); } else { digits.splice(0, overage); } } return digits; }, startCycle: function() { var self = this; setInterval(function() { for (var i=0; i
 
' + '
 
' + '
 
' + '
 
', increment: function(speed) { var next = this.pos + 1; if (next >= this.options.chars.length) { next = 0; } this.$prev.html(this.options.chars[this.pos]).show(); this.$front_bottom.hide(); this.$next.html(this.options.chars[next]); var speed1 = Math.floor(Math.random() * speed * .4 + speed * .3); var speed2 = Math.floor(Math.random() * speed * .1 + speed * .2); if (speed >= this.options.threshhold) { if (this.options.transform) { this.animateSlow(speed1, speed2); } else { this.animateFast(speed1, speed2); } } this.pos = next; }, animateSlow: function(speed1, speed2) { var _this = this; this.$back_top.show(); this.$front_bottom.transform({ scaleY: 0.0 }); this.$front_top.transform({ scaleY: 1.0 }).stop().show().animate({ scaleY: 0.0 }, speed1, 'swing', function(){ _this.$front_bottom.stop().show().animate({ scaleY: 1.0 }, speed2, 'linear'); _this.$front_top.hide().transform({ scaleY: 1.0 }); }); }, animateFast: function(speed1, speed2) { var _this = this; if (this.timeout) { clearTimeout(this.timeout); } this.timeout = setTimeout(function(){ _this.$front_top.hide(); _this.timeout = setTimeout(function(){ _this.$front_bottom.show(); }, speed2); }, speed1); }, goToPosition: function(pos) { var _this = this; var frameFunc = function() { if (_this.timing_timer) { clearInterval(_this.timing_timer); _this.timing_timer = null; } var distance = pos - _this.pos; if (distance <0) { distance += _this.options.chars.length; } if (_this.pos == pos) { clearInterval(_this.timing_timer); _this.timing_timer = null; _this.$ele.trigger("digitAnimEnd"); } else { var duration = Math.floor( (_this.options.timing - _this.options.min_timing) / distance + _this.options.min_timing ); _this.increment(duration); _this.timing_timer = setTimeout(frameFunc, duration); } } frameFunc(); }, goToNextPosition: function() { var next = this.pos + 1; if (next >= this.options.chars.length) { next = 0; } this.goToPosition(next); }, goToChar: function(c) { var pos = $.inArray(c, this.options.chars); if (pos == -1) { this.options.chars.push(c); pos = this.options.chars.length - 1; } this.goToPosition(pos); } }; $.fn.flapper = function(options) { this.each(function(){ new Flapper($(this), options); }); return this; } })(jQuery);