-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtouch-slider.html
More file actions
332 lines (269 loc) · 8.39 KB
/
touch-slider.html
File metadata and controls
332 lines (269 loc) · 8.39 KB
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
<link rel="import" href="../polymer/polymer.html">
<link rel="import" href="../iron-flex-layout/iron-flex-layout.html">
<link rel="import" href="../iron-image/iron-image.html">
<!--
`touch-slider` is a super simple draggable slider for touch devices.
The drag doesn't work with mouse events. IMO for desktop it's better to have some arrow handles instead.
A slide is a container where you can attach content. There is always three slides attached at the same time, so animations can occur.
Example:
<touch-slider items="[[images]]"></touch-slider>
@demo demo/index.html
-->
<dom-module id="touch-slider">
<template>
<style>
:host {
display: block;
overflow: hidden;
position: relative;
-moz-user-select: -moz-none;
-khtml-user-select: none;
-webkit-user-select: none;
-o-user-select: none;
user-select: none;
}
#slides {
@apply(--layout-horizontal);
@apply(--layout-wrap);
bottom: 0;
left: calc(-100% - 20px);
overflow: hidden;
position: absolute;
top: 0;
width: calc(300% + 40px);
will-change: transform;
}
.slide {
@apply(--layout-center-center);
@apply(--layout-flex);
@apply(--layout);
height: 100%;
pointer-events: none;
position: relative;
}
.slide:first-child {
margin-right: 20px;
}
.slide:last-child {
margin-left: 20px;
}
</style>
<div id="slides">
</div>
</template>
</dom-module>
<script>
(function() {
'use strict';
Polymer({
is: 'touch-slider',
properties: {
items: {
type: Array,
observer: '_itemsChanged'
},
/**
* Current visible slide index.
*/
currentIndex: {
type: Number,
value: 0,
notify: true,
observer: '_currentIndexChanged'
},
/**
* True if the user is dragging a slide.
*/
dragging: {
type: Boolean,
value: false,
readOnly: true,
reflectToAttribute: true
},
/**
* When true the slider is not draggable.
*/
dragDisabled: {
type: Boolean,
value: false,
notify: true,
reflectToAttribute: true
},
/**
* When a drag ends and a new currentIndex is set, the container is transformed
* to the new position with a smooth animation. When the animation finishes this value
* is set to match the currentIndex and it's then when the dom elements are reattached
* to match the current set of indexes.
*/
_domCurrentIndex: {
type: Number,
observer: '_domCurrentIndexChanged'
}
},
listeners: {
'touchstart': '_onTouchStart',
'touchmove': '_onTouchMove',
'touchend': '_onTouchEnd'
},
attached: function() {
this._currentIndexChanged(this.currentIndex);
},
_itemsChanged: function() {
this.$.slides.innerHTML = '';
this.currentIndex = 0;
this._domCurrentIndexChanged(this.currentIndex);
},
_onTouchStart: function(event) {
this._startX = event.touches[0].clientX;
},
_onTouchMove: function(event) {
if (typeof this._startX === 'number' && !this.disabled) {
var dx = event.touches[0].clientX - this._startX;
if (this.dragging) {
if (Math.abs(dx) <= this._rect.width) {
requestAnimationFrame(function() {
this.$.slides.style.transform = 'translate3d(' + dx + 'px, 0, 0)';
}.bind(this));
}
this._dx = dx;
} else {
// Don't start dragging until the finger has moved a bit
if (Math.abs(dx) > 4) {
this._updateRect();
this._startDraggingTime = new Date().getTime();
this._setDragging(true);
}
}
}
},
_onTouchEnd: function(event) {
if (this.dragging) {
event.stopPropagation();
var currentTime = new Date().getTime();
this._draggingDuration = currentTime - this._startDraggingTime;
var newIndex = this.currentIndex;
if (this._draggingDuration / Math.abs(this._dx) < 1.2) {
newIndex = this._dx > 0 ? this.currentIndex - 1 : this.currentIndex + 1;
}
if (newIndex === this.currentIndex) {
this._currentIndexChanged(newIndex, newIndex);
} else {
this.currentIndex = newIndex;
}
this._startX = null;
this._dx = null;
this._startDraggingTime = null;
this._setDragging(false);
}
},
/**
* Animate the current slide to the new currentIndex.
*/
_currentIndexChanged: function(currentIndex, old) {
if (!this.isAttached || !this.items || !this.items.length) {
return;
}
// Index should be between 0 and the max value
this.currentIndex = Math.min(Math.max(0, currentIndex), Math.max(0, this.items.length - 1));
if (this.currentIndex !== currentIndex) {
return;
}
// Which direction the slides are moving to.
var direction;
if (currentIndex > old && old !== -1) {
direction = 'right';
} else if (currentIndex < old && old !== this.items.length) {
direction = 'left';
} else {
direction = 'center';
}
if (this._animation) {
this._animation.finish();
}
// Only animate on touch
if (this.dragging) {
this._updateRect();
var isFastDrag = this._draggingDuration < 70;
var translateX = 0;
if (direction === 'left') {
translateX = this._rect.width + 20;
} else if (direction === 'right') {
translateX = -this._rect.width -20;
}
this._animation = this.$.slides.animate([
{transform: this.$.slides.style.transform},
{transform: 'translate3d(' + translateX + 'px, 0, 0)'}
], {
duration: isFastDrag < 70 ? 150 : 400,
easing: isFastDrag < 70 ? 'cubic-bezier(0.39, 0.575, 0.565, 1)': 'cubic-bezier(0.215, 0.61, 0.355, 1)'
});
this._animation.onfinish = function() {
this._domCurrentIndex = currentIndex;
this.$.slides.style.transform = '';
this._animation = null;
}.bind(this);
} else {
// Dont animate for if triggered by mouse events
this._domCurrentIndex = currentIndex;
}
},
/**
* Reattach the slides to match the current index set.
*
* @param {!Number} domCurrentIndex
*/
_domCurrentIndexChanged: function(domCurrentIndex) {
var currentIndexes = [];
for (var i = domCurrentIndex - 1; i <= domCurrentIndex + 1; i++) {
currentIndexes.push(i);
}
var slides = this.$.slides.children;
var unchangedIndexes = [];
// Remove unnecessary slides
for (var i = slides.length - 1; i >= 0; i--) {
var slide = slides[i];
var slideIndex = Number(slide.dataset.index);
if (currentIndexes.indexOf(slideIndex) < 0) {
slide.remove();
} else {
unchangedIndexes.push(slideIndex);
}
}
// Add slides
for (var i = 0; i < 3; i++) {
var index = currentIndexes[i];
if (unchangedIndexes.indexOf(index) < 0) {
this._renderSlide(currentIndexes[i], i);
}
}
},
/**
* @param {!Number} index - The index of the slide.
* @param {!Number} slideIndex - Represents the slide child index. It can be 0, 1 or 2.
*/
_renderSlide: function(index, slideIndex) {
var slide = this.create('div');
slide.classList.add('slide');
slide.setAttribute('data-index', index);
if (index >= 0 && this.items && this.items[index]) {
var ironImage = this.create('iron-image', {
src: this.items[index],
sizing: 'cover'
});
ironImage.style.height = '100%';
ironImage.style.width = '100%';
Polymer.dom(slide).appendChild(ironImage);
}
var slideInThatPosition = this.$.slides.children[slideIndex];
if (slideIndex < 2 && slideInThatPosition) {
Polymer.dom(this.$.slides).insertBefore(slide, slideInThatPosition);
} else {
Polymer.dom(this.$.slides).appendChild(slide);
}
},
_updateRect: function() {
this._rect = this.getBoundingClientRect();
}
});
}());
</script>