From e1f8c06c8c894aa29f0c76fcc640d0322164e7dd Mon Sep 17 00:00:00 2001 From: Ashish Rawat Date: Fri, 1 Mar 2024 09:32:23 +0530 Subject: [PATCH 1/2] Add auto slide support to tp slider --- src/slider/index.html | 29 +++++++++++++++++++++++++++++ src/slider/tp-slider.ts | 13 +++++++++++++ 2 files changed, 42 insertions(+) diff --git a/src/slider/index.html b/src/slider/index.html index b115bc4..ecfbab9 100644 --- a/src/slider/index.html +++ b/src/slider/index.html @@ -188,6 +188,35 @@ 1 / 4 + +
+ + + + + + + + + + + +

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.

+

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.

+

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.

+

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.

+

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.

+
+
+
+ + + + + + + 1 / 4 +
diff --git a/src/slider/tp-slider.ts b/src/slider/tp-slider.ts index febe7be..182f299 100644 --- a/src/slider/tp-slider.ts +++ b/src/slider/tp-slider.ts @@ -31,6 +31,19 @@ export class TPSliderElement extends HTMLElement { this.slide(); this.setAttribute( 'initialized', 'yes' ); + // Auto Slide. + const autoSlide = this.getAttribute( 'auto-slide' ); + + // If auto slide is enabled, then we need to start the interval. + if ( 'yes' === autoSlide ) { + const interval = this.getAttribute( 'auto-slide-interval' ); + + // Start the interval. + setInterval( () => { + this.next(); + }, parseInt( interval ?? '4000' ) ); + } + // Event listeners. if ( ! ( 'ResizeObserver' in window ) ) { // We set the resize observer in `tp-slider-slide` From e8a1c2fdcb02e2ea8ffa1b04cdba770c1ac21bd1 Mon Sep 17 00:00:00 2001 From: Junaid Bhura Date: Fri, 1 Mar 2024 15:29:16 +1100 Subject: [PATCH 2/2] update auto sliding --- src/slider/README.md | 22 ++++++++++++---------- src/slider/index.html | 2 +- src/slider/tp-slider.ts | 41 ++++++++++++++++++++++++++++------------- 3 files changed, 41 insertions(+), 24 deletions(-) diff --git a/src/slider/README.md b/src/slider/README.md index fdc7a8d..030a287 100644 --- a/src/slider/README.md +++ b/src/slider/README.md @@ -53,19 +53,21 @@ slider.setCurrentSlide( 2 ); ## Attributes -| Attribute | Required | Values | Notes | -|-----------------|----------|-----------------|--------------------------------------------------------------------------------------------------------| -| flexible-height | No | `yes` | Whether the height of the slider changes depending on the content inside the slides | -| infinite | No | `yes` | Go back to the first slide at the end of all slides, and open the last slide when navigating backwards | -| swipe | No | `yes` | Whether to add support for swiping gestures on touch devices | -| behaviour | No | `fade`, `slide` | The default behaviour is to slide between slides. This can be updated to fade. | +| Attribute | Required | Values | Notes | +|---------------------|----------|-----------------|--------------------------------------------------------------------------------------------------------| +| flexible-height | No | `yes` | Whether the height of the slider changes depending on the content inside the slides | +| infinite | No | `yes` | Go back to the first slide at the end of all slides, and open the last slide when navigating backwards | +| swipe | No | `yes` | Whether to add support for swiping gestures on touch devices | +| behaviour | No | `fade`, `slide` | The default behaviour is to slide between slides. This can be updated to fade. | +| auto-slide-interval | No | | Interval in milliseconds. | ## Events -| Event | Notes | -|----------------|---------------------------------------------------| -| slide-set | When the current slide is set, but before sliding | -| slide-complete | After sliding is complete | +| Event | Notes | +|---------------------|--------------------------------------------------| +| slide-set | When the current slide is set, but before sliding | +| slide-complete | After sliding is complete | +| auto-slide-complete | After auto sliding is complete | ## Methods diff --git a/src/slider/index.html b/src/slider/index.html index ecfbab9..8322268 100644 --- a/src/slider/index.html +++ b/src/slider/index.html @@ -192,7 +192,7 @@
- + diff --git a/src/slider/tp-slider.ts b/src/slider/tp-slider.ts index 182f299..cf24f58 100644 --- a/src/slider/tp-slider.ts +++ b/src/slider/tp-slider.ts @@ -29,21 +29,9 @@ export class TPSliderElement extends HTMLElement { // Initialize slider. this.slide(); + this.autoSlide(); this.setAttribute( 'initialized', 'yes' ); - // Auto Slide. - const autoSlide = this.getAttribute( 'auto-slide' ); - - // If auto slide is enabled, then we need to start the interval. - if ( 'yes' === autoSlide ) { - const interval = this.getAttribute( 'auto-slide-interval' ); - - // Start the interval. - setInterval( () => { - this.next(); - }, parseInt( interval ?? '4000' ) ); - } - // Event listeners. if ( ! ( 'ResizeObserver' in window ) ) { // We set the resize observer in `tp-slider-slide` @@ -420,4 +408,31 @@ export class TPSliderElement extends HTMLElement { this.next(); } } + + /** + * Auto slide. + */ + protected autoSlide(): void { + // Auto Slide. + const autoSlideInterval: string | null = this.getAttribute( 'auto-slide-interval' ); + + // Check if we have an auto slider interval. + if ( ! autoSlideInterval ) { + return; + } + + // Check for a valid interval. + const interval: number = parseInt( autoSlideInterval ); + if ( interval <= 0 ) { + return; + } + + // Run this on a timeout, rather than interval, so the interval can be controlled after + // the component is initialised. + setTimeout( (): void => { + this.next(); + this.autoSlide(); + this.dispatchEvent( new CustomEvent( 'auto-slide-complete' ) ); + }, parseInt( autoSlideInterval ) ); + } }