Subversion-Projekte lars-tiefland.ci

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
776 lars 1
/*!
2
* jquery.counterup.js 1.0
3
*
4
* Copyright 2013, Benjamin Intal http://gambit.ph @bfintal
5
* Released under the GPL v2 License
6
*
7
* Date: Nov 26, 2013
8
*/
9
(function( $ ){
10
  "use strict";
11
 
12
  $.fn.counterUp = function( options ) {
13
 
14
    // Defaults
15
    var settings = $.extend({
16
        'time': 400,
17
        'delay': 10
18
    }, options);
19
 
20
    return this.each(function(){
21
 
22
        // Store the object
23
        var $this = $(this);
24
        var $settings = settings;
25
 
26
        var counterUpper = function() {
27
            var divisions = $settings.time / $settings.delay;
28
            var num = $this.attr('data-value');
29
            var nums = [num];
30
            var isComma = /[0-9]+,[0-9]+/.test(num);
31
            num = num.replace(/,/g, '');
32
            var isInt = /^[0-9]+$/.test(num);
33
            var isFloat = /^[0-9]+\.[0-9]+$/.test(num);
34
            var decimalPlaces = isFloat ? (num.split('.')[1] || []).length : 0;
35
 
36
            // Generate list of incremental numbers to display
37
            for (var i = divisions; i >= 1; i--) {
38
 
39
                // Preserve as int if input was int
40
                var newNum = parseInt(num / divisions * i);
41
 
42
                // Preserve float if input was float
43
                if (isFloat) {
44
                    newNum = parseFloat(num / divisions * i).toFixed(decimalPlaces);
45
                }
46
 
47
                // Preserve commas if input had commas
48
                if (isComma) {
49
                    while (/(\d+)(\d{3})/.test(newNum.toString())) {
50
                        newNum = newNum.toString().replace(/(\d+)(\d{3})/, '$1'+','+'$2');
51
                    }
52
                }
53
 
54
                nums.unshift(newNum);
55
            }
56
 
57
            $this.data('counterup-nums', nums);
58
            $this.text('0');
59
 
60
            // Updates the number until we're done
61
            var f = function() {
62
                $this.text($this.data('counterup-nums').shift());
63
                if ($this.data('counterup-nums').length) {
64
                    setTimeout($this.data('counterup-func'), $settings.delay);
65
                } else {
66
                    delete $this.data('counterup-nums');
67
                    $this.data('counterup-nums', null);
68
                    $this.data('counterup-func', null);
69
                }
70
            };
71
            $this.data('counterup-func', f);
72
 
73
            // Start the count up
74
            setTimeout($this.data('counterup-func'), $settings.delay);
75
        };
76
 
77
        // Perform counts when the element gets into view
78
        $this.waypoint(counterUpper, { offset: '100%', triggerOnce: true });
79
    });
80
 
81
  };
82
 
83
})( jQuery );