-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathindex.js
91 lines (72 loc) · 2.29 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
/**
* Module dependencies
*/
var domify = require( 'domify' )
, classes = require('classes')
, has3d = require('has-translate3d')
, each = require('each')
, transformProperty = require( 'transform-property' );
/**
* Expose `MinimalCounter`
*/
module.exports = MinimalCounter;
/**
*
*/
function MinimalCounter( value ){
var self = this;
this.intervals = [];
this.prevShifts = [];
this.el = domify( '<div class="minimal-counter"/>' );
this.value = value || 100;
this.value.toString().split('').forEach( self.addDigit.bind( this ) );
this.update( this.value );
}
MinimalCounter.prototype.addDigit = function() {
var digit = domify( '<div class="digit"/>' ),
sequence = domify( '<div class="sequence">'
+ [9, 8, 7, 6, 5, 4, 3, 2, 1, 0].join('\n')
+ '</div>' );
digit.appendChild( sequence );
this.el.appendChild( digit );
};
MinimalCounter.prototype.update = function( number ) {
var self = this,
digits = number.toString().split('').reverse(),
digitElements = this.el.children,
diff = digitElements.length - digits.length;
if( diff < 0 ){
while ( diff++ ){ this.addDigit(); }
digitElements = this.el.children;
} else {
while ( diff-- ){ digits.push( -1 ); }
}
for ( var index = 0; index < digits.length; index += 1 ) {
var num = digits[index],
shift = - ( 9 - parseInt( num, 10 ) ) * 10,
elIndex = digitElements.length - index - 1,
element = digitElements[elIndex].children[0],
translateValue = has3d ? "translate3d(0, " + shift + "%, 0)" : "translate(0, " + shift + "%)";
if( num === -1 ){
classes( element ).add( 'is-hidden' );
} else {
classes( element ).remove( 'is-hidden' );
if (transformProperty) {
this.setTransform(element, translateValue);
} else {
this.animate(element, this.prevShifts[index], shift, index);
}
}
this.prevShifts[index] = shift;
}
};
MinimalCounter.prototype.setTransform = function( element, translateValue ) {
element.style[transformProperty] = translateValue;
};
/*
* This should animate from previous
*/
MinimalCounter.prototype.animate = function(element, prevShiftY, shiftY, index) {
// TODO
//
};