@@ -11,6 +11,11 @@ import { TPSliderArrowElement } from './tp-slider-arrow';
1111 * TP Slider.
1212 */
1313export class TPSliderElement extends HTMLElement {
14+ /**
15+ * Properties.
16+ */
17+ protected touchStartX : number = 0 ;
18+
1419 /**
1520 * Constructor.
1621 */
@@ -28,6 +33,8 @@ export class TPSliderElement extends HTMLElement {
2833
2934 // Event listeners.
3035 window . addEventListener ( 'resize' , this . handleResize . bind ( this ) ) ;
36+ this . addEventListener ( 'touchstart' , this . handleTouchStart . bind ( this ) ) ;
37+ this . addEventListener ( 'touchend' , this . handleTouchEnd . bind ( this ) ) ;
3138 }
3239
3340 /**
@@ -271,4 +278,39 @@ export class TPSliderElement extends HTMLElement {
271278 _this . removeAttribute ( 'resizing' ) ;
272279 } , 10 ) ;
273280 }
281+
282+ /**
283+ * Detect touch start event, and store the starting location.
284+ *
285+ * @param {Event } e Touch event.
286+ *
287+ * @protected
288+ */
289+ protected handleTouchStart ( e : TouchEvent ) : void {
290+ if ( 'yes' === this . getAttribute ( 'swipe' ) ) {
291+ this . touchStartX = e . touches [ 0 ] . clientX ;
292+ }
293+ }
294+
295+ /**
296+ * Detect touch end event, and check if it was a left or right swipe.
297+ *
298+ * @param {Event } e Touch event.
299+ *
300+ * @protected
301+ */
302+ protected handleTouchEnd ( e : TouchEvent ) : void {
303+ if ( 'yes' !== this . getAttribute ( 'swipe' ) ) {
304+ return ;
305+ }
306+
307+ const touchEndX : number = e . changedTouches [ 0 ] . clientX ;
308+ const swipeDistance : number = touchEndX - this . touchStartX ;
309+
310+ if ( swipeDistance > 0 ) {
311+ this . previous ( ) ;
312+ } else if ( swipeDistance < 0 ) {
313+ this . next ( ) ;
314+ }
315+ }
274316}
0 commit comments