-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbasic-sliding-viewport-fit.html
152 lines (125 loc) · 3.94 KB
/
basic-sliding-viewport-fit.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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
<!--
A variant of basic-sliding-viewport which automatically sizes itself to
accommodate the tallest/widest items in a list.
@element basic-sliding-viewport-fit
-->
<!-- TODO: Rationalize this with basic-sliding-viewport -->
<link rel="import" href="../basic-aspect/basic-aspect.html">
<dom-module id="basic-sliding-viewport-fit">
<template>
<style>
:host {
display: block;
overflow: hidden;
position: relative;
}
#slidingContainer {
height: 100%;
position: absolute;
will-change: transform;
}
:host(.snapTransition) #slidingContainer {
-webkit-transition: -webkit-transform 0.2s ease-out;
transition: transform 0.2s ease-out;
}
</style>
<div id="slidingContainer" role="presentation">
<content></content>
</div>
</template>
</dom-module>
<script>
Polymer({
behaviors: [Basic.Aspect],
contribute: {
/**
* The fractional position of the element's moving surface while it is being
* moved (dragged/scrolled/etc.).
*
* This is expressed as a fraction of the element's width. If the value is
* positive, the surface is being moved to the left; if negative, the surface is
* being moved to the right. E.g., a value of 0.5 indicates the surface has
* moved half the element's width to the left.
*
* @property position
* @type Number
*/
get position() {
return this._position;
},
set position(position) {
this._position = position;
this.render();
},
set selectedItem(item) {
this.render();
},
touchEnd: function() {
this.classList.add('snapTransition');
},
touchStart: function() {
this.classList.remove('snapTransition');
}
},
is: 'basic-sliding-viewport-fit',
ready: function() {
this.classList.add('snapTransition');
},
render: function() {
requestAnimationFrame(this._renderSelection.bind(this));
},
_renderSelection: function() {
var count = this.collective.items && this.collective.items.length;
if (!count) {
// Null or zero means we don't have items to render yet.
return;
}
var index = this.collective.selectedIndex;
if (index < 0) {
// No selection
// return;
index = 0;
}
var position = this.collective.position || 0;
var dampenedPosition;
if (index === 0 && position < 0) {
// Apply tension from the left edge.
dampenedPosition = -this._damping(-position);
} else if (index === count - 1 && position > 0) {
// Apply tension from the right edge.
dampenedPosition = this._damping(position);
} else {
// No damping required.
dampenedPosition = position;
}
var fractionalIndex = index + dampenedPosition;
// Use a percentage so the transform will still work if screen size changes
// (e.g., if device orientation changes).
// var left = -fractionalIndex * 100;
var left = -(fractionalIndex / count) * 100;
var transform = 'translateX(' + left + '%)';
this.$.slidingContainer.style.webkitTransform = transform;
this.$.slidingContainer.style.transform = transform;
},
/*
* Calculate damping as a function of the distance past the minimum/maximum
* values.
*
* We want to asymptotically approach an absolute minimum of 1 unit
* below/above the actual minimum/maximum. This requires calculating a
* hyperbolic function.
*
* See http://www.wolframalpha.com/input/?i=y+%3D+-1%2F%28x%2B1%29+%2B+1
* for the one we use. The only portion of that function we care about is when
* x is zero or greater. An important consideration is that the curve be
* tangent to the diagonal line x=y at (0, 0). This ensures smooth continuity
* with the normal drag behavior, in which the visible sliding is linear with
* the distance the touchpoint has been dragged.
*/
_damping: function(x) {
var y = (-1 / (x + 1)) + 1;
return y;
},
_position: 0
});
</script>