@@ -79,11 +79,11 @@ export class InViewportDirective implements AfterViewInit, OnDestroy {
7979 /**
8080 * A parent element to listen to scroll events from
8181 *
82- * @type {HTMLElement }
82+ * @type {* }
8383 * @memberof InViewportDirective
8484 */
8585 @Input ( 'snInViewportParent' )
86- public parentEl : HTMLElement ;
86+ public parentEl : any ;
8787 /**
8888 * Returns true if element is in viewport
8989 *
@@ -178,7 +178,7 @@ export class InViewportDirective implements AfterViewInit, OnDestroy {
178178 * @memberof InViewportDirective
179179 */
180180 public calculateInViewportStatus ( ) : void {
181- const el : HTMLElement = this . el . nativeElement ;
181+ const el = this . el . nativeElement ;
182182 let inParentViewport = false ;
183183 let inWindowViewport = false ;
184184
@@ -201,24 +201,30 @@ export class InViewportDirective implements AfterViewInit, OnDestroy {
201201 * Returns true if an element is currently within the `viewport`
202202 *
203203 * @param {Viewport } viewport
204- * @param {HTMLElement } el
204+ * @param {* } el
205205 * @returns {boolean }
206206 * @memberof InViewportDirective
207207 */
208- public isInElementViewport ( viewport : Viewport , el : HTMLElement ) : boolean {
209- const elBounds = el . getBoundingClientRect ( ) ;
210- return (
211- (
212- ( elBounds . top >= viewport . top ) && ( elBounds . top <= viewport . bottom ) ||
213- ( elBounds . bottom >= viewport . top ) && ( elBounds . bottom <= viewport . bottom ) ||
214- ( elBounds . top <= viewport . top ) && ( elBounds . bottom >= viewport . bottom )
215- ) &&
216- (
217- ( elBounds . left >= viewport . left ) && ( elBounds . left <= viewport . right ) ||
218- ( elBounds . right >= viewport . left ) && ( elBounds . right <= viewport . right ) ||
219- ( elBounds . left <= viewport . left && elBounds . right >= viewport . right )
220- )
221- ) ;
208+ public isInElementViewport ( viewport : Viewport , el : any ) : boolean {
209+ // Check if `getBoundingClientRect` is a function in case running this code
210+ // in an evironment without the DOM
211+ if ( typeof el . getBoundingClientRect === 'function' ) {
212+ const elBounds = el . getBoundingClientRect ( ) ;
213+ return (
214+ (
215+ ( elBounds . top >= viewport . top ) && ( elBounds . top <= viewport . bottom ) ||
216+ ( elBounds . bottom >= viewport . top ) && ( elBounds . bottom <= viewport . bottom ) ||
217+ ( elBounds . top <= viewport . top ) && ( elBounds . bottom >= viewport . bottom )
218+ ) &&
219+ (
220+ ( elBounds . left >= viewport . left ) && ( elBounds . left <= viewport . right ) ||
221+ ( elBounds . right >= viewport . left ) && ( elBounds . right <= viewport . right ) ||
222+ ( elBounds . left <= viewport . left && elBounds . right >= viewport . right )
223+ )
224+ ) ;
225+ } else {
226+ return false ;
227+ }
222228 }
223229 /**
224230 * trigger `ngUnsubscribe` complete on
0 commit comments