forked from KanoComputing/x-carousel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathx-carousel.html
256 lines (251 loc) · 8.71 KB
/
x-carousel.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
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
<link rel="import" type="" href="../polymer/polymer.html">
<!--
## x-carousel
This component is a responsive, touch-enabled, scrollable carousel. Put in
some elements and style it as you wish.
### Example
<style>
--x-carousel-button {
background: red;
}
}
</style>
<x-carousel>
<div>One</div>
<div>Two</div>
<div>Three</div>
</x-carousel>
@demo demo/x-carousel.html
-->
<dom-module id="x-carousel">
<style>
:host {
display: flex;
display: -webkit-flex;
display: -ms-flexbox;
}
.button-slot {
transition: opacity ease 0.5s;
-webkit-transition: opacity ease 0.5s;
-ms-transition: opacity ease 0.5s;
z-index: 1;
}
.hidden {
visibility: hidden;
opacity: 0;
}
#viewport {
flex: 1 0 auto;
-webkit-flex: 1 0 auto;
-ms-flex: 1 0 auto;
display: flex;
display: -webkit-flex;
display: -ms-flexbox;
}
#viewport.non-touch {
overflow: hidden;
}
#viewport.touch {
overflow-x: scroll;
-webkit-overflow-scrolling: touch;
-ms-scroll-chaining: chained;
}
#content {
flex: 1 0 auto;
-webkit-flex: 1 0 auto;
-ms-flex: 1 0 auto;
display: flex;
display: -webkit-flex;
display: -ms-flexbox;
flex-direction: row;
-webkit-flex-direction: row;
-ms-flex-direction: row;
flex-wrap: nowrap;
-webkit-flex-wrap: nowrap;
-ms-flex-wrap: nowrap;
overflow: visible;
}
.ease {
transition: all ease 0.5s;
-webkit-transition: all ease 0.5s;
-ms-transition: all ease 0.5s;
}
</style>
<template>
<div class$="button-slot [[applyHiddenClass(isTouch, buttonsNeeded)]]" on-tap="_leftTap">
<slot name="left-button"></slot>
</div>
<div id="viewport" class$="{{applyOverflowVisibility(isTouch)}}"
on-mousedown="dragStart" on-mousemove="dragMove"
on-mouseup="dragStop" on-mouseout="dragCancel">
<div id="content" class$="{{applyEaseClass(moving)}}">
<slot name="content"></slot>
</div>
</div>
<div class$="button-slot [[applyHiddenClass(isTouch, buttonsNeeded)]]" on-tap="_rightTap">
<slot name="right-button"></slot>
</div>
</template>
<script type="text/javascript">
Polymer({
is: 'x-carousel',
properties: {
offset: {
type: Number,
value: 0,
notify: true
},
moving: {
type: Boolean,
value: false,
notify: true
},
isTouch: {
type: Boolean,
value: false,
notify: true,
observer: '_updateUI'
},
withMouseScroll: {
type: Boolean,
value: false
},
buttonsNeeded: {
type: Boolean,
value: false,
notify: true
}
},
_leftTap: function (e) {
var target = e.path ? e.path[0] : e.target;
if (target.hasAttribute('carousel-control-left')) {
this.moveLeft();
}
},
_rightTap: function (e) {
var target = e.path ? e.path[0] : e.target;
if (target.hasAttribute('carousel-control-right')) {
this.moveRight();
}
},
moveLeft: function () {
this.offset += this.$.viewport.offsetWidth * 0.75;
this.translate(this.offset);
this.enforceBoundaries();
},
moveRight: function () {
this.offset -= this.$.viewport.offsetWidth * 0.75;
this.translate(this.offset);
this.enforceBoundaries();
},
translate: function (offset) {
this.$.content.style.transform = `translateX(${offset}px)`;
},
dragStart: function (event) {
if (this.withMouseScroll) {
this.moving = true;
this.lastX = event.clientX;
}
},
dragMove: function (event) {
if (this.moving) {
this.offset += (event.clientX - this.lastX);
this.lastX = event.clientX;
this.translate(this.offset);
}
},
dragStop: function (event) {
if (this.moving) {
this.moving = false;
this.enforceBoundaries();
}
},
dragCancel: function (event) {
if (this.moving) {
var viewport = this.$.viewport,
x = viewport.offsetLeft,
y = viewport.offsetTop,
w = viewport.offsetWidth,
h = viewport.offsetHeight;
if ((event.clientX <= x || event.clientX >= (x + w)) ||
(event.clientY <= y || event.clientY >= (y + h))) {
this.stop(event);
}
}
},
onMouseOver: function (event) {
this.updateNeedButtons(true);
},
onMouseOut: function (event) {
this.updateNeedButtons(false);
},
enforceBoundaries: function () {
var nodes = this.$,
contentWidth = nodes.content.scrollWidth,
viewportWidth = nodes.viewport.offsetWidth;
if (this.offset > 0) {
this.offset = 0;
} else if (Math.abs(this.offset) > contentWidth - viewportWidth) {
this.offset = -(contentWidth - viewportWidth);
}
this.updateNeedButtons();
this.translate(this.offset);
},
updateNeedButtons: function (mouseInside) {
var nodes = this.$,
contentWidth = nodes.content.scrollWidth,
viewportWidth = nodes.viewport.offsetWidth;
if (mouseInside === undefined) {
mouseInside = this.buttonsNeeded;
}
this.buttonsNeeded = (contentWidth > viewportWidth) && mouseInside;
},
applyEaseClass: function (moving) {
if (!moving) {
return 'ease';
}
return '';
},
applyHiddenClass: function (isTouch, buttonsNeeded) {
if (!isTouch && buttonsNeeded) {
return '';
} else {
return 'hidden';
}
},
applyOverflowVisibility: function (isTouch) {
return isTouch ? 'touch' : 'non-touch';
},
_updateUI: function () {
if (this.isTouch) {
this.removeListeners();
}
},
removeListeners: function () {
window.removeEventListener('resize', this.enforceBoundaries);
this.removeEventListener('mouseover', this.onMouseOver);
this.removeEventListener('mouseout', this.onMouseOut);
},
applyTouchBehaviour: function () {
this.isTouch = true;
window.removeEventListener('touchstart', this.applyTouchBehaviour);
},
created: function () {
this.applyTouchBehaviour = this.applyTouchBehaviour.bind(this);
},
attached: function () {
window.addEventListener('resize',
this.enforceBoundaries.bind(this));
this.addEventListener('mouseover', this.onMouseOver.bind(this));
this.addEventListener('mouseout', this.onMouseOut.bind(this));
//update carousel behaviour on touch devices
window.addEventListener('touchstart', this.applyTouchBehaviour);
},
detached: function () {
if (!this.isTouch) {
this.removeListeners();
}
}
});
</script>
</dom-module>