/* * jquery.appear * https://github.com/bas2k/jquery.appear/ * http://code.google.com/p/jquery-appear/ * * copyright (c) 2009 michael hixson * copyright (c) 2012 alexander brovikov * licensed under the mit license (http://www.opensource.org/licenses/mit-license.php) */ (function ($) { $.fn.appear = function (fn, options) { var settings = $.extend({ //arbitrary data to pass to fn data: undefined, //call fn only on the first appear? one: true, // x & y accuracy accx: 0, accy: 0 }, options); return this.each(function () { var t = $(this); //whether the element is currently visible t.appeared = false; if (!fn) { //trigger the custom event t.trigger('appear', settings.data); return; } var w = $(window); //fires the appear event when appropriate var check = function () { //is the element hidden? if (!t.is(':visible')) { //it became hidden t.appeared = false; return; } //is the element inside the visible window? var a = w.scrollleft(); var b = w.scrolltop(); var o = t.offset(); var x = o.left; var y = o.top; var ax = settings.accx; var ay = settings.accy; var th = t.height(); var wh = w.height(); var tw = t.width(); var ww = w.width(); if (y + th + ay >= b && y <= b + wh + ay && x + tw + ax >= a && x <= a + ww + ax) { //trigger the custom event if (!t.appeared) t.trigger('appear', settings.data); } else { //it scrolled out of view t.appeared = false; } }; //create a modified fn with some additional logic var modifiedfn = function () { //mark the element as visible t.appeared = true; //is this supposed to happen only once? if (settings.one) { //remove the check w.unbind('scroll', check); var i = $.inarray(check, $.fn.appear.checks); if (i >= 0) $.fn.appear.checks.splice(i, 1); } //trigger the original fn fn.apply(this, arguments); }; //bind the modified fn to the element if (settings.one) t.one('appear', settings.data, modifiedfn); else t.bind('appear', settings.data, modifiedfn); //check whenever the window scrolls w.scroll(check); //check whenever the dom changes $.fn.appear.checks.push(check); //check now (check)(); }); }; //keep a queue of appearance checks $.extend($.fn.appear, { checks: [], timeout: null, //process the queue checkall: function () { var length = $.fn.appear.checks.length; if (length > 0) while (length--)($.fn.appear.checks[length])(); }, //check the queue asynchronously run: function () { if ($.fn.appear.timeout) cleartimeout($.fn.appear.timeout); $.fn.appear.timeout = settimeout($.fn.appear.checkall, 20); } }); //run checks when these methods are called $.each(['append', 'prepend', 'after', 'before', 'attr', 'removeattr', 'addclass', 'removeclass', 'toggleclass', 'remove', 'css', 'show', 'hide' ], function (i, n) { var old = $.fn[n]; if (old) { $.fn[n] = function () { var r = old.apply(this, arguments); $.fn.appear.run(); return r; } } }); })(jquery); (function (factory) { if (typeof define === 'function' && define.amd) { // amd define(['jquery'], factory); } else if (typeof exports === 'object') { // commonjs factory(require('jquery')); } else { // browser globals factory(jquery); } }(function ($) { var countto = function (element, options) { this.$element = $(element); this.options = $.extend({}, countto.defaults, this.dataoptions(), options); this.init(); }; countto.defaults = { from: 0, // the number the element should start at to: 0, // the number the element should end at speed: 1000, // how long it should take to count between the target numbers refreshinterval: 100, // how often the element should be updated decimals: 0, // the number of decimal places to show formatter: formatter, // handler for formatting the value before rendering onupdate: null, // callback method for every time the element is updated oncomplete: null // callback method for when the element finishes updating }; countto.prototype.init = function () { this.value = this.options.from; this.loops = math.ceil(this.options.speed / this.options.refreshinterval); this.loopcount = 0; this.increment = (this.options.to - this.options.from) / this.loops; }; countto.prototype.dataoptions = function () { var from = 0; var to = parsefloat(this.$element.text().tostring().replace(/,/g, "")); if(this.$element.data("from")){ from = parsefloat(this.$element.data("from").tostring().replace(/,/g, "")); }; if(this.$element.data("to")){ to = parsefloat(this.$element.data("to").tostring().replace(/,/g, "")); }; var decimals = 0; var decimalsarr = to.tostring().split("."); if (decimalsarr[1]) { decimals = decimalsarr[1].tostring().length; }; var options = { from: from, to: to, speed: this.$element.data('speed'), refreshinterval: this.$element.data('refresh-interval'), decimals: decimals }; var keys = object.keys(options); for (var i in keys) { var key = keys[i]; if (typeof (options[key]) === 'undefined') { delete options[key]; } } return options; }; countto.prototype.update = function () { this.value += this.increment; this.loopcount++; this.render(); if (typeof (this.options.onupdate) == 'function') { this.options.onupdate.call(this.$element, this.value); } if (this.loopcount >= this.loops) { clearinterval(this.interval); this.value = this.options.to; if (typeof (this.options.oncomplete) == 'function') { this.options.oncomplete.call(this.$element, this.value); } } }; countto.prototype.render = function () { var formattedvalue = this.options.formatter.call(this.$element, this.value, this.options); this.$element.text(formattedvalue); }; countto.prototype.restart = function () { this.stop(); this.init(); this.start(); }; countto.prototype.start = function () { this.stop(); this.render(); this.interval = setinterval(this.update.bind(this), this.options.refreshinterval); }; countto.prototype.stop = function () { if (this.interval) { clearinterval(this.interval); } }; countto.prototype.toggle = function () { if (this.interval) { this.stop(); } else { this.start(); } }; function formatter(value, options) { return value.tofixed(options.decimals); } $.fn.countto = function (option) { return this.each(function () { var $this = $(this); var data = $this.data('countto'); var init = !data || typeof (option) === 'object'; var options = typeof (option) === 'object' ? option : {}; var method = typeof (option) === 'string' ? option : 'start'; if (init) { if (data) data.stop(); $this.data('countto', data = new countto(this, options)); } data[method].call(data); }); }; }));