Skip to content

Commit 382e141

Browse files
Merge pull request #49 from Travelopia/feature/Add-auto-slide-support-to-tp-slider
Add auto slide support to TP slider
2 parents d54652d + e8a1c2f commit 382e141

File tree

3 files changed

+69
-10
lines changed

3 files changed

+69
-10
lines changed

src/slider/README.md

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -53,19 +53,21 @@ slider.setCurrentSlide( 2 );
5353

5454
## Attributes
5555

56-
| Attribute | Required | Values | Notes |
57-
|-----------------|----------|-----------------|--------------------------------------------------------------------------------------------------------|
58-
| flexible-height | No | `yes` | Whether the height of the slider changes depending on the content inside the slides |
59-
| 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 |
61-
| behaviour | No | `fade`, `slide` | The default behaviour is to slide between slides. This can be updated to fade. |
56+
| Attribute | Required | Values | Notes |
57+
|---------------------|----------|-----------------|--------------------------------------------------------------------------------------------------------|
58+
| flexible-height | No | `yes` | Whether the height of the slider changes depending on the content inside the slides |
59+
| 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 |
61+
| behaviour | No | `fade`, `slide` | The default behaviour is to slide between slides. This can be updated to fade. |
62+
| auto-slide-interval | No | <interval> | Interval in milliseconds. |
6263

6364
## Events
6465

65-
| Event | Notes |
66-
|----------------|---------------------------------------------------|
67-
| slide-set | When the current slide is set, but before sliding |
68-
| slide-complete | After sliding is complete |
66+
| Event | Notes |
67+
|---------------------|--------------------------------------------------|
68+
| slide-set | When the current slide is set, but before sliding |
69+
| slide-complete | After sliding is complete |
70+
| auto-slide-complete | After auto sliding is complete |
6971

7072
## Methods
7173

src/slider/index.html

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,35 @@
188188
</tp-slider-nav>
189189
<tp-slider-count current="1" total="4" format="$current / $total">1 / 4</tp-slider-count>
190190
</tp-slider>
191+
192+
<br>
193+
194+
<!--Slider that autoplay -->
195+
<tp-slider flexible-height="yes" infinite="yes" auto-slide-interval="3000">
196+
<tp-slider-arrow direction="previous"><button>&laquo; Previous</button></tp-slider-arrow>
197+
<tp-slider-arrow direction="next"><button>Next &raquo;</button></tp-slider-arrow>
198+
<tp-slider-track>
199+
<tp-slider-slides>
200+
<tp-slider-slide><img src="https://picsum.photos/600/300" width="600" height="300" alt=""></tp-slider-slide>
201+
<tp-slider-slide><img src="https://picsum.photos/600/300" width="600" height="300" alt=""></tp-slider-slide>
202+
<tp-slider-slide><img src="https://picsum.photos/600/300" width="600" height="300" alt=""></tp-slider-slide>
203+
<tp-slider-slide>
204+
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
205+
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
206+
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
207+
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
208+
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
209+
</tp-slider-slide>
210+
</tp-slider-slides>
211+
</tp-slider-track>
212+
<tp-slider-nav>
213+
<tp-slider-nav-item><button>1</button></tp-slider-nav-item>
214+
<tp-slider-nav-item><button>2</button></tp-slider-nav-item>
215+
<tp-slider-nav-item><button>3</button></tp-slider-nav-item>
216+
<tp-slider-nav-item><button>4</button></tp-slider-nav-item>
217+
</tp-slider-nav>
218+
<tp-slider-count current="1" total="4" format="$current / $total">1 / 4</tp-slider-count>
219+
</tp-slider>
191220
</main>
192221
</body>
193222
</html>

src/slider/tp-slider.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ export class TPSliderElement extends HTMLElement {
2929

3030
// Initialize slider.
3131
this.slide();
32+
this.autoSlide();
3233
this.setAttribute( 'initialized', 'yes' );
3334

3435
// Event listeners.
@@ -407,4 +408,31 @@ export class TPSliderElement extends HTMLElement {
407408
this.next();
408409
}
409410
}
411+
412+
/**
413+
* Auto slide.
414+
*/
415+
protected autoSlide(): void {
416+
// Auto Slide.
417+
const autoSlideInterval: string | null = this.getAttribute( 'auto-slide-interval' );
418+
419+
// Check if we have an auto slider interval.
420+
if ( ! autoSlideInterval ) {
421+
return;
422+
}
423+
424+
// Check for a valid interval.
425+
const interval: number = parseInt( autoSlideInterval );
426+
if ( interval <= 0 ) {
427+
return;
428+
}
429+
430+
// Run this on a timeout, rather than interval, so the interval can be controlled after
431+
// the component is initialised.
432+
setTimeout( (): void => {
433+
this.next();
434+
this.autoSlide();
435+
this.dispatchEvent( new CustomEvent( 'auto-slide-complete' ) );
436+
}, parseInt( autoSlideInterval ) );
437+
}
410438
}

0 commit comments

Comments
 (0)