Skip to content

Commit 3fbe074

Browse files
add basic swiping gestures
1 parent b49d6db commit 3fbe074

File tree

3 files changed

+45
-2
lines changed

3 files changed

+45
-2
lines changed

src/slider/README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ slider.setCurrentSlide( 2 );
3232
```
3333

3434
```html
35-
<tp-slider flexible-height="yes" infinite="yes">
35+
<tp-slider flexible-height="yes" infinite="yes" swipe="yes">
3636
<tp-slider-arrow direction="previous"><button>&laquo; Previous</button></tp-slider-arrow> <-- There must be a button inside this component
3737
<tp-slider-arrow direction="next"><button>Next &raquo;</button></tp-slider-arrow> <-- There must be a button inside this component
3838
<tp-slider-track>
@@ -57,6 +57,7 @@ slider.setCurrentSlide( 2 );
5757
|-----------------|----------|--------|--------------------------------------------------------------------------------------------------------|
5858
| flexible-height | No | `yes` | Whether the height of the slider changes depending on the content inside the slides |
5959
| infinite | No | `yes` | Go back to the first slide at the end of all slides, and open the last slide when navigating backwards |
60+
| swipe | No | `yes` | Whether to add support for swiping gestures on touch devices |
6061

6162
## Events
6263

src/slider/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
</head>
3333
<body>
3434
<main>
35-
<tp-slider flexible-height="yes" infinite="yes">
35+
<tp-slider flexible-height="yes" infinite="yes" swipe="yes">
3636
<tp-slider-arrow direction="previous"><button>&laquo; Previous</button></tp-slider-arrow>
3737
<tp-slider-arrow direction="next"><button>Next &raquo;</button></tp-slider-arrow>
3838
<tp-slider-track>

src/slider/tp-slider.ts

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,11 @@ import { TPSliderArrowElement } from './tp-slider-arrow';
1111
* TP Slider.
1212
*/
1313
export 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

Comments
 (0)