Skip to content

Commit e8a1c2f

Browse files
update auto sliding
1 parent e1f8c06 commit e8a1c2f

File tree

3 files changed

+41
-24
lines changed

3 files changed

+41
-24
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: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@
192192
<br>
193193

194194
<!--Slider that autoplay -->
195-
<tp-slider flexible-height="yes" auto-slide="yes" infinite="yes" auto-slide-interval="3000">
195+
<tp-slider flexible-height="yes" infinite="yes" auto-slide-interval="3000">
196196
<tp-slider-arrow direction="previous"><button>&laquo; Previous</button></tp-slider-arrow>
197197
<tp-slider-arrow direction="next"><button>Next &raquo;</button></tp-slider-arrow>
198198
<tp-slider-track>

src/slider/tp-slider.ts

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

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

34-
// Auto Slide.
35-
const autoSlide = this.getAttribute( 'auto-slide' );
36-
37-
// If auto slide is enabled, then we need to start the interval.
38-
if ( 'yes' === autoSlide ) {
39-
const interval = this.getAttribute( 'auto-slide-interval' );
40-
41-
// Start the interval.
42-
setInterval( () => {
43-
this.next();
44-
}, parseInt( interval ?? '4000' ) );
45-
}
46-
4735
// Event listeners.
4836
if ( ! ( 'ResizeObserver' in window ) ) {
4937
// We set the resize observer in `tp-slider-slide`
@@ -420,4 +408,31 @@ export class TPSliderElement extends HTMLElement {
420408
this.next();
421409
}
422410
}
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+
}
423438
}

0 commit comments

Comments
 (0)