-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtehapo-odometer-digit.html
115 lines (100 loc) · 2.8 KB
/
tehapo-odometer-digit.html
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
<dom-module id="tehapo-odometer-digit">
<template>
<style>
:host {
display: inline-block;
position: relative;
overflow: hidden;
line-height: 1;
height: 1em;
@apply(--tehapo-odometer-digit);
}
#next {
position: absolute;
}
.animate {
transition: all 400ms;
}
</style>
<div id="next">[[next]]</div>
<div id="current" on-transitionend="_transitionEnd">[[current]]</div>
</template>
<script>
Polymer({
is: 'tehapo-odometer-digit',
properties: {
value: {
type: Number,
observer: '_animate'
},
current: {
type: Number
},
next: {
type: Number
},
_transitioning: {
type: Boolean
},
_buffer: {
type: Array,
value: function() {
return [];
}
}
},
attached: function() {
this.transform('translateY(-100%)', this.$.next);
this.transform('translateY(0)', this.$.current);
},
_setAnimationEnabled: function(enabled) {
this.toggleClass('animate', enabled, this.$.next);
this.toggleClass('animate', enabled, this.$.current);
},
_flushCssChanges: function() {
// Trigger a reflow, flushing CSS changes.
var ignored = this.$.next.offsetHeight;
},
_transitionEnd: function() {
this.current = this.next;
this._setAnimationEnabled(false);
this.transform('translateY(-100%)', this.$.next);
this.transform('translateY(0)', this.$.current);
this._flushCssChanges();
this._transitioning = false;
if (this._buffer.length > 0) {
this._animate(this._buffer.shift());
}
},
_animate: function(nextValue) {
if (this.current === undefined) {
this.current = nextValue;
return;
}
if (nextValue === this.current) {
return;
}
if (this._transitioning) {
this._buffer.push(nextValue);
return;
}
this._transitioning = true;
this._setAnimationEnabled(false);
this.next = nextValue;
// Set starting position.
var translateCurrent = '100%';
if (this.next > this.current || this.next === 0) {
this.transform('translateY(-100%)', this.$.next);
} else {
translateCurrent = '-100%';
this.transform('translateY(100%)', this.$.next);
}
this._flushCssChanges();
// Do the animation.
this._setAnimationEnabled(true);
this.transform('translateY(0)', this.$.next);
this.transform('translateY(' + translateCurrent + ')', this.$.current);
}
});
</script>
</dom-module>